在R中使用循环列表计算交货周期中的天数

huangapple go评论60阅读模式
英文:

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 &lt;- list(&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;)

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(&quot;Friday&quot;, &quot;Tuesday&quot;)
# [1] 4 

day_diff(&quot;Saturday&quot;, &quot;Sunday&quot;)
# [1] 1

day_diff(&quot;Sunday&quot;, &quot;Saturday&quot;)
# [1]  6 

答案2

得分: 0

以下是翻译好的部分:

你可以这样做:

DaysOfWeek &lt;- c(&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;)

delivery_start &lt;- &#39;Sunday&#39;
delivery_end &lt;- &#39;Tuesday&#39;

delivery_days &lt;- ifelse(which(DaysOfWeek == delivery_start) &gt; 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 &lt;- c(&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;)

delivery_start &lt;- &#39;Sunday&#39;
delivery_end &lt;- &#39;Tuesday&#39;

delivery_days &lt;- ifelse(which(DaysOfWeek == delivery_start) &gt; 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 &lt;- rep(c(&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;), 2)

days_between &lt;- function(day1, day2) {
  start &lt;- which(DayofWeek == day1)
  end &lt;- which(DayofWeek == day2)
  
  min(Filter(\(x) x &gt; 0, sapply(end, `-`, start)))
}

Output

days_between(&quot;Monday&quot;, &quot;Saturday&quot;)
[1] 5
days_between(&quot;Sunday&quot;, &quot;Monday&quot;)
[1] 1

huangapple
  • 本文由 发表于 2023年5月24日 23:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定