I've got some survey data for multiple-selection questions that has been output with a column for each possible selection, populated with TRUE/FALSE values, e.g.
library(dplyr)
dat <- tribble(
~name, ~state, ~onions, ~`sweet potatoes`, ~garlic,
"Tom", "WV", TRUE, FALSE, TRUE,
"Larry", "NC", FALSE, TRUE, FALSE,
"Beth", "NY", TRUE, TRUE, TRUE
)
dat
#> name state onions `sweet potatoes` garlic
#> <chr> <chr> <lgl> <lgl> <lgl>
#> 1 Tom WV TRUE FALSE TRUE
#> 2 Larry NC FALSE TRUE FALSE
#> 3 Beth NY TRUE TRUE TRUE
How can I create a list-column containing the column names for which the respondents answered "TRUE"?
simliar to https://stackoverflow.com/a/9508203/1009730
I've tried several variations on the theme of
dat %>%
rowwise() %>%
mutate(fav_foods = list(names(which(~ is.logical(.x) && .x))))
which gives the error:
Error: Problem with
mutate()inputfav_foods. x argument to 'which' is not logical i Inputfav_foodsislist(names(which(~is.logical(.x) && .x))). i The error occurred in row 1.