I am working with an untidy registration form for a summer camp. The form output is given below:
leaders teen_adventure
1 camp, overnight <NA>
2 <NA> <NA>
3 camp, overnight camp, float, overnight
I want to generate new columns that sum the tally for each possible answer.
leaders teen_adventure camps overnights floats
1 camp, overnight <NA> 1 1 0
2 <NA> <NA> 0 0 0
3 camp, overnight camp, float, overnight 2 2 1
I feel in my bones that this has a dplyr solution, something like:
reprex %>%
mutate(camps = sum(case_when(
str_detect(select(., everything()), "camp") ~ 1,
TRUE ~ 0
)))
or perhaps using across().
Here is the sample data set:
# data
reprex <- structure(list(leaders = c("camp, overnight", NA, "camp, overnight"),
teen_adventure = c(NA, NA, "camp, float, overnight")),
row.names = c(NA, -3L), class = "data.frame")