Say, I have the following 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
I need to add one more column days (7 days) following the number of week. Say for the above data.frame
, it would be like this:
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
The following works:
library(dplyr)
xYMW %>% dplyr::mutate(day = lubridate::wday(date))
If you input option label = TRUE
, you can also turn this into abbreviated or non-abbreviated labels of Monday, Tuesday, ...