0

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.

psysky
  • 3,037
  • 5
  • 28
  • 64

1 Answers1

1

Using your example I just expand on it. Depending on your needs, use a left_join or a full_join. I used a full_join so "International Women's Day" is shown in the outcome.

I clean the name using as.character since in your example it is a factor. If name is not a factor, as.character is not needed. In the end I remove No_holidays.

df %>% full_join(df1) %>% 
  mutate(weekDay = lubridate::dmy(Data) %>% weekdays(),
         name = ifelse(is.na(name), "No_Holiday", as.character(name)), 
         holiday = ifelse(is.na(name), 0, 1),
         value = 1) %>%
  spread(key = weekDay, value=value, fill=0) %>% 
  spread(key = name, value = holiday, fill = 0) %>% 
  select(-No_Holiday)

         Data  Y Friday Monday Saturday Sunday Thursday Tuesday Wednesday International Women's Day New Year
1  01.01.2018  8      0      1        0      0        0       0         0                         0        1
2  02.01.2018  9      0      0        0      0        0       1         0                         0        0
3  03.01.2018 10      0      0        0      0        0       0         1                         0        0
4  08.03.2018 NA      0      0        0      0        1       0         0                         1        0
5  25.12.2017  1      0      1        0      0        0       0         0                         0        0
6  26.12.2017  2      0      0        0      0        0       1         0                         0        0
7  27.12.2017  3      0      0        0      0        0       0         1                         0        0
8  28.12.2017  4      0      0        0      0        1       0         0                         0        0
9  29.12.2017  5      1      0        0      0        0       0         0                         0        0
10 30.12.2017  6      0      0        1      0        0       0         0                         0        0
11 31.12.2017  7      0      0        0      1        0       0         0                         0        0
phiver
  • 23,048
  • 14
  • 44
  • 56