为星期和年份创建向量

时间:2019-06-02 20:57:35

标签: r

我需要为一年中的某几天(1-365)创建一个向量,然后从星期一开始为其指定一周中的几天。我可以做第一部分,但似乎无法弄清楚如何将星期几(星期一,星期二...)与之合并,以便它们在打印时重复一年:

all.days = c(1:365)
days.week = (days=c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday","Sunday")

1 个答案:

答案 0 :(得分:0)

这将提供一个长为365的向量,从星期一开始,并给出一周的连续几天。基于以下事实:2018-01-01是星期一。

all.days <- 1:365
format(as.Date("2018-01-01") + all.days - 1, "%A")

给予:

[1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
[7] "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[13] "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday" 
... etc ...