Ordering days of the week (consecutive days) – 排列星期几(连续的天)

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

Ordering days of the week (consecutive days)

问题

  1. 我有一个包含一些星期几的向量,我需要它们按顺序排列,我有一种感觉可能有一个可以处理星期的包。
  2. mydays
  3. "星期五" "星期一" "星期六" "星期天"
  4. 我希望它们变成这样。在这里也值得注意的是,这会有点不同,我不能像通常那样按星期几排序,因为星期一会排在第一位(我不想要这样),我的日期总是连续的,这可能会使它更容易排序。
  5. mydays
  6. "星期五" "星期六" "星期天" "星期一"
  7. 我尝试了但似乎不起作用,因为没有东西可以排序,R 不知道星期五和星期六之间的区别,例如。
  8. mydays[order(mydays)]
英文:

I have a vector of some days of the week which I need to be in order, I have a feeling there might be a package that deal with days but could well be wrong.

  1. mydays
  2. "Friday" "Monday" "Saturday" "Sunday"

I want these to become. Also worth noting here that this will be a bit different and i cant just normally order by days of the week as Monday will be first (I don't want this), my days will always be consecutive days which might make it a bit easer to order

  1. mydays
  2. "Friday" "Saturday" "Sunday" "Monday"

I've tried buy looks like it doesnt work as theres nothing to order on as R doesn't know any difference between Friday and Saturday for example.

  1. mydays[order(mydays)]

答案1

得分: 2

更新: 我们可以创建一个星期几的向量,并将其用作因子水平:

  1. # 你的字符字符串向量
  2. mydays <- c("Friday","Monday","Saturday","Sunday" )
  3. # 转换为因子
  4. mydays <- factor(mydays, levels = weekdays(ISOdate(1, 1, 1:7)))
  5. # 对因子排序
  6. sort(mydays)
  7. # [1] Monday Friday Saturday Sunday
  8. # Levels: Monday Tuesday Wednesday Thursday Friday Saturday Sunday

第一个答案:
首先转换为因子,然后排序:

  1. # 你的字符字符串向量
  2. mydays <- c("Friday","Monday","Saturday","Sunday" )
  3. # 转换为因子
  4. mydays <- factor(mydays, levels = c("Friday", "Saturday", "Sunday", "Monday"))
  5. # 对因子排序
  6. sort(mydays)

输出:

  1. [1] Friday Saturday Sunday Monday
  2. Levels: Friday Saturday Sunday Monday
英文:

Update: We could create a vector of weekdays and use it as factor levels:

  1. # your character string vector
  2. mydays &lt;- c(&quot;Friday&quot;,&quot;Monday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot; )
  3. # transfrom to factor
  4. mydays &lt;- factor(mydays, levels = weekdays(ISOdate(1, 1, 1:7)))
  5. # order the factor
  6. sort(mydays)
  7. # [1] Monday Friday Saturday Sunday
  8. # Levels: Monday Tuesday Wednesday Thursday Friday Saturday Sunday

First answer:
First convert to factor then order:

  1. # your character string vector
  2. mydays &lt;- c(&quot;Friday&quot;,&quot;Monday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot; )
  3. # transfrom to factor
  4. mydays &lt;- factor(mydays, levels = c(&quot;Friday&quot;, &quot;Saturday&quot;, &quot;Sunday&quot;, &quot;Monday&quot;))
  5. # order the factor
  6. sort(mydays)

output:

  1. [1] Friday Saturday Sunday Monday
  2. Levels: Friday Saturday Sunday Monday

答案2

得分: 2

我们可以根据输入向量中第一天的匹配来旋转因子水平,在您的示例中,它是星期五:

  1. #旋转向量的函数
  2. #https://stackoverflow.com/a/30542172/680068
  3. shifter <- function(x, n = 1) {
  4. if (n == 0) x else c(tail(x, -n), head(x, n))
  5. }
  6. # 输入
  7. mydays <- c("星期五","星期一","星期六","星期日" )
  8. # 获取工作日
  9. wd <- weekdays(ISOdate(1, 1, 1:7))
  10. # 基于起始日进行移位
  11. wdShift <- shifter(wd, which(wd == mydays[1]) - 1)
  12. # 转换为因子
  13. mydays <- factor(mydays, levels = wdShift)
  14. # 对因子排序
  15. sort(mydays)
  16. # [1] 星期五 星期六 星期日 星期一
  17. # Levels: 星期五 星期六 星期日 星期一 星期二 星期三 星期四
英文:

We can rotate the factor levels based on match of the first day of the week in the input vector, in your example, it is Friday:

  1. #function to rotate vector
  2. #https://stackoverflow.com/a/30542172/680068
  3. shifter &lt;- function(x, n = 1) {
  4. if (n == 0) x else c(tail(x, -n), head(x, n))
  5. }
  6. # input
  7. mydays &lt;- c(&quot;Friday&quot;,&quot;Monday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot; )
  8. # get weekdays
  9. wd &lt;- weekdays(ISOdate(1, 1, 1:7))
  10. # shift based on starting day
  11. wdShift &lt;- shifter(wd, which(wd == mydays[1]) - 1)
  12. # transfrom to factor
  13. mydays &lt;- factor(mydays, levels = wdShift)
  14. # order the factor
  15. sort(mydays)
  16. # [1] Friday Saturday Sunday Monday
  17. # Levels: Friday Saturday Sunday Monday Tuesday Wednesday Thursday

huangapple
  • 本文由 发表于 2023年7月20日 17:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728374.html
匿名

发表评论

匿名网友

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

确定