This post related with last post transform date into dummy variable in R, but more complex. I have data
df=structure(list(Data = structure(c(4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L), .Label = c("01.01.2018", "02.01.2018", "03.01.2018",
"25.12.2017", "26.12.2017", "27.12.2017", "28.12.2017", "29.12.2017",
"30.12.2017", "31.12.2017"), class = "factor"), Y = 1:10), .Names = c("Data",
"Y"), class = "data.frame", row.names = c(NA, -10L))
I had to transform days of date into dummy variable. If the day refers to this date, then one, otherwise 0.
The solution provided by Paweł Kozielski-Romaneczko helped me.
library(dplyr)
library(lubridate)
library(tidyr)
df %>%
mutate(weekDay = lubridate::dmy(Data) %>% weekdays(),
value = 1) %>%
spread(key=weekDay, value=value, fill=0)
But now, i must add columns with holidays. I.E. Is the date holiday or not?
i have auxiliary dataset where indicated is the date is holiday?
df1=structure(list(Data = structure(1:2, .Label = c("01.01.2018",
"08.03.2018"), class = "factor"), name = structure(c(2L, 1L), .Label = c("International Women's Day",
"New Year"), class = "factor")), .Names = c("Data", "name"), class = "data.frame", row.names = c(NA,
-2L))
so as output i need this holiday
Data Y Mon Tue Wed Thu Fri Sat Sun New Year International Women's Day
25.12.2017 1 1 0 0 0 0 0 0 0 0
26.12.2017 2 0 1 0 0 0 0 0 0 0
27.12.2017 3 0 0 1 0 0 0 0 0 0
28.12.2017 4 0 0 0 1 0 0 0 0 0
29.12.2017 5 0 0 0 0 1 0 0 0 0
30.12.2017 6 0 0 0 0 0 1 0 0 0
31.12.2017 7 0 0 0 0 0 0 1 0 0
01.01.2018 8 1 0 0 0 0 0 0 1 0
02.01.2018 9 0 1 0 0 0 0 0 0 0
03.01.2018 10 0 0 1 0 0 0 0 0 0
How can i add the holidays as dummy variables with name taken from auxiliary dataset?
P.S. If you consider that this topic must be in my last post, just let me know, i ll delete it.