英文:
Using a circular list in R to calculate days in a delivery period
问题
在R中创建一个循环列表
我正在处理一个交付时间表,需要计算每个唯一交付周期中的天数。最初我试图在Excel中解决我的相关问题,但现在我考虑改用R。
如果我有一周中的日期列表:
DaysOfWeek <- list("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
我希望能够始终引用该列表并计算未来的天数。
例如,如果交付时间表是交付周期1:星期五至星期一,周期2:星期二至星期四。
我希望能够引用我的列表,说交付日1是星期五,然后向前计算,直到下一个交付日,星期二,并查看该周期中有四天的交付日。
我尝试过硬编码特定日期并将它们设置为一周中的日期,还尝试了几种不同的for循环,但仍然遇到困难。
英文:
Creating a circular list in R
I am working with a delivery schedule where I need to calculate the days in each unique delivery period. I was trying to solve my related issue in excel originally, but am now thinking I need to use R instead.
If I have list of the days of the week:
DaysOfWeek <- list("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
I always want to be able to reference the list and calculate days forward.
For example, if the delivery schedule is Delivery period 1: Friday-Monday, and period 2: is Tuesday-Thursday.
I want to be able to reference my list say delivery day 1 is Friday, count forward until the next delivery day, Tuesday, and see that there are four delivery days in that period.
I have tried hard coding specific dates and setting them to the days of the week as well as several different for loops and am still struggling.
答案1
得分: 2
如果我理解你的需求,我们可以使用模数运算。
我会将一周的天数编码为一个从0开始的命名向量,然后使用一个小的辅助函数来计算差值模7。
days = c(
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
)
day_diff = function(from, to) {
unname((days[to] - days[from]) %% 7)
}
day_diff("Friday", "Tuesday")
# [1] 4
day_diff("Saturday", "Sunday")
# [1] 1
day_diff("Sunday", "Saturday")
# [1] 6
英文:
If I understand what you want, we can use modular arithmetic.
I would code the days of the week as a named vector starting at 0 and then use a little helper function to calculate the differences mod 7.
days = c(
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
)
day_diff = function(from, to) {
unname((days[to] - days[from]) %% 7)
}
day_diff("Friday", "Tuesday")
# [1] 4
day_diff("Saturday", "Sunday")
# [1] 1
day_diff("Sunday", "Saturday")
# [1] 6
答案2
得分: 0
以下是翻译好的部分:
你可以这样做:
DaysOfWeek <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
delivery_start <- 'Sunday'
delivery_end <- 'Tuesday'
delivery_days <- ifelse(which(DaysOfWeek == delivery_start) > which(DaysOfWeek == delivery_end),
7 + which(DaysOfWeek == delivery_end) - which(DaysOfWeek == delivery_start),
which(DaysOfWeek == delivery_end) - which(DaysOfWeek == delivery_start))
这个示例会返回结果 2
。
英文:
You could do:
DaysOfWeek <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
delivery_start <- 'Sunday'
delivery_end <- 'Tuesday'
delivery_days <- ifelse(which(DaysOfWeek == delivery_start) > which(DaysOfWeek == delivery_end),
7 + which(DaysOfWeek == delivery_end) - which(DaysOfWeek == delivery_start),
which(DaysOfWeek == delivery_end) - which(DaysOfWeek == delivery_start))
This example would return the result 2
.
答案3
得分: 0
以下是翻译好的部分:
这里有一种可能性,您可以将一周的天向量翻倍,然后找到两天之间的最短正距离:
DayofWeek <- rep(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"), 2)
days_between <- function(day1, day2) {
start <- which(DayofWeek == day1)
end <- which(DayofWeek == day2)
min(Filter(\(x) x > 0, sapply(end, `-`, start)))
}
输出
days_between("Monday", "Saturday")
[1] 5
days_between("Sunday", "Monday")
[1] 1
英文:
Here is a possibility where you double your days of the week vector and then find the shortest positive distance between two days:
DayofWeek <- rep(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"), 2)
days_between <- function(day1, day2) {
start <- which(DayofWeek == day1)
end <- which(DayofWeek == day2)
min(Filter(\(x) x > 0, sapply(end, `-`, start)))
}
Output
days_between("Monday", "Saturday")
[1] 5
days_between("Sunday", "Monday")
[1] 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论