I have a dataframe:
df <- data.frame(date = c('2021-01-05','2021-01-05','2021-01-10'),
creature =c('bird','dog','dog'))
I'd like to use pivot_wider to create column names for every individual item in creature, then populate the dataframe with TRUE or FALSE depending on whether that thing exists for that date or not.
date creature bird dog
<chr> <chr> <lgl> <lgl>
1 2021-01-05 bird TRUE TRUE
2 2021-01-05 dog TRUE TRUE
3 2021-01-10 dog FALSE TRUE
I can do this with dplyr, however, if there were dozens of levels, this would be annoying to do. Is there a way to do this with pivot_wider?
df %>% group_by(date) %>%
mutate(bird = ifelse('bird' %in% creature,TRUE,FALSE),
dog = ifelse('dog' %in% creature, TRUE,FALSE))