添加新的列天

时间:2018-08-13 07:03:40

标签: r lubridate days cbind

说,我有以下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

我需要在星期数之后再增加一列天数(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

2 个答案:

答案 0 :(得分:1)

以下作品:

library(dplyr)
xYMW %>% dplyr::mutate(day = lubridate::wday(date))

如果输入选项label = TRUE,则也可以将其转换为星期一,星期二,...的缩写或非缩写标签

答案 1 :(得分:0)

尝试一下:

xYMW<-xYMW %>% group_by(week) %>% mutate(day=row_number())
相关问题