Using magrittr to change a subset of values

I have a data time frame with a day of the week column. I would like to replace all Mondays (1st day) which are 6th to Sunday holidays without breaking my pipeline with magrittr.

Without pipelines, it looks like this:

dates = c("5/24/15", "5/25/15", "5/26/15", "5/27/15", "5/28/15", "5/29/15", "5/30/15")
df <- data.frame(date = as.POSIXct(dates,format = "%m/%d/%y"), day = 0:6, value = 1:7)
holidays <- c("2015-05-25")
df$day[df$date %in% as.POSIXct(holidays)] <- 6

      

But I would like to do something like this:

df <- df %>%
  filter(value < 30) %>%
  mutate(new_variable = something) %>%
  REPLACE HOLIDAYS WITH SUNDAY HERE

      

+3


source to share


1 answer


use% in% to make an index and then replace with mutat. I think:



df %>% mutate(day=replace(day, date %in% as.POSIXct(holidays), 6))

      

+2


source







All Articles