比方说,我有以下data.frame
require(lubridate)
Dates<-seq(as.Date('2017/01/01'), by = 'day', length.out = 365)
xYMW <- data.frame(date=(Dates), month=month(Dates), week=week(Dates))
xYMW[1:15,]
date month week
1 2017-01-01 1 1
2 2017-01-02 1 1
3 2017-01-03 1 1
4 2017-01-04 1 1
5 2017-01-05 1 1
6 2017-01-06 1 1
7 2017-01-07 1 1
8 2017-01-08 1 2
9 2017-01-09 1 2
10 2017-01-10 1 2
11 2017-01-11 1 2
12 2017-01-12 1 2
13 2017-01-13 1 2
14 2017-01-14 1 2
15 2017-01-15 1 3我需要在周数之后再添加一列days (7天)。假设上面的data.frame是这样的:
date month week day
1 2017-01-01 1 1 1
2 2017-01-02 1 1 2
3 2017-01-03 1 1 3
4 2017-01-04 1 1 4
5 2017-01-05 1 1 5
6 2017-01-06 1 1 6
7 2017-01-07 1 1 7
8 2017-01-08 1 2 1
9 2017-01-09 1 2 2
10 2017-01-10 1 2 3
11 2017-01-11 1 2 4
12 2017-01-12 1 2 5
13 2017-01-13 1 2 6
14 2017-01-14 1 2 7
15 2017-01-15 1 3 1发布于 2018-08-13 15:10:14
以下是工作原理:
library(dplyr)
xYMW %>% dplyr::mutate(day = lubridate::wday(date))如果输入选项label = TRUE,还可以将其转换为星期一、星期二……的缩写或非缩写标签。
发布于 2018-08-13 15:11:41
试试这个:
xYMW<-xYMW %>% group_by(week) %>% mutate(day=row_number())https://stackoverflow.com/questions/51816789
复制相似问题