添加新列天数
Adding a new column days
说,我有以下 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
以下作品:
library(dplyr)
xYMW %>% dplyr::mutate(day = lubridate::wday(date))
如果您输入选项label = TRUE
,您还可以将其转换为星期一、星期二...的缩写或非缩写标签
试试这个:
xYMW<-xYMW %>% group_by(week) %>% mutate(day=row_number())
说,我有以下 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
以下作品:
library(dplyr)
xYMW %>% dplyr::mutate(day = lubridate::wday(date))
如果您输入选项label = TRUE
,您还可以将其转换为星期一、星期二...的缩写或非缩写标签
试试这个:
xYMW<-xYMW %>% group_by(week) %>% mutate(day=row_number())