R为一系列数字分配周值

时间:2015-07-06 21:04:00

标签: r intervals

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

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

我的目标是为序列前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个并分配“第1周”,然后计算下5个并指定“第2周”......还有其他方法吗?

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

非常感谢!

3 个答案:

答案 0 :(得分:2)

以下是week包的lubridate函数的解决方案。请注意,在传递到week函数列date之前,需要使用ymd作为POSIX类进行转换。

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

答案 1 :(得分:2)

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

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

将“日期”列转换为“日期”类后,其他选项将使用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

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

答案 2 :(得分:1)

以下是使用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"