R为一系列数字分配周值

R assign week value for a range of numbers

我有一个如下所示的数据框:

dataset <- data.frame(date = seq(from=as.Date("2015-07-06"),
                                 to=as.Date("2015-07-15"),by="day"),
                      stringsAsFactors=F)

我的 objective 是将周值分配给前 5 个日期的序列,因此它看起来像这样:

          date   week
 1: 2015-07-06 Week 1
 2: 2015-07-07 Week 1
 3: 2015-07-08 Week 1
 4: 2015-07-09 Week 1
 5: 2015-07-10 Week 1
 6: 2015-07-11 Week 2
 7: 2015-07-12 Week 2
 8: 2015-07-13 Week 2
 9: 2015-07-14 Week 2
10: 2015-07-15 Week 2

我的数据只是工作日数据,因此只有 5 天。每周从星期一开始...只是为了提供一些背景信息。

除了计算前 5 个并分配 "Week 1",然后计算下 5 个并分配 "Week 2"...等等之外,还有其他方法吗?

我将这篇文章放在一个 for 循环中,所以我希望有一个直接的解决方案。

非常感谢!

这是我使用 lubridate 包的 week 函数的解决方案。请注意,在传递给 week 函数之前,列 date 需要用 ymd 转换为 POSIX class.

library(lubridate)
dataset$date <- ymd(dataset$date)
dataset$week <- week(dataset$date)
dataset
         date week
1  2015-07-06   27
2  2015-07-07   27
3  2015-07-08   28
4  2015-07-09   28
5  2015-07-10   28
6  2015-07-11   28
7  2015-07-12   28
8  2015-07-13   28
9  2015-07-14   28
10 2015-07-15   29

由于'date'列只有工作日且没有任何休息,我们可以使用gl/paste创建周索引。这不依赖于数据集的 nrow 即即使 nrow 不是 5 的倍数,它也会起作用。

 dataset$week <- paste('Week', as.numeric(gl(nrow(dataset),5, nrow(dataset))))

其他选项将在将 'date' 列转换为 'Date' class.

后使用 format
format(as.Date(dataset$date),'%W')
#[1] "27" "27" "27" "27" "27" "27" "27" "28" "28" "28"

week(strptime(dataset$date,format='%Y-%m-%d'))
#[1] 27 27 28 28 28 28 28 28 28 29

但是,我不确定这是否是您想要的。

这是一个使用 base R 的简单解决方案:

nweeks <- 10 #choose as required
days <- paste0("Week",rep(seq(nweeks),each=5))
#> days
# [1] "Week1"  "Week1"  "Week1"  "Week1"  "Week1"  "Week2"  "Week2"  "Week2"  "Week2"  "Week2"  "Week3"  "Week3"  "Week3"  "Week3"  "Week3" 
#[16] "Week4"  "Week4"  "Week4"  "Week4"  "Week4"  "Week5"  "Week5"  "Week5"  "Week5"  "Week5"  "Week6"  "Week6"  "Week6"  "Week6"  "Week6" 
#[31] "Week7"  "Week7"  "Week7"  "Week7"  "Week7"  "Week8"  "Week8"  "Week8"  "Week8"  "Week8"  "Week9"  "Week9"  "Week9"  "Week9"  "Week9" 
#[46] "Week10" "Week10" "Week10" "Week10" "Week10"