I have a dataset with symptoms (20+), the symptoms are categorized as Yes/No/Unknown. I would like to create a new column which indicates if the subject (ID) has no symptoms (I'm defining this as they have no symptoms with 'Yes').
I've got a sample dataset below and I can create a column as desired but it feels like there must be a better/cleaner way just using dplyr::mutate() rather than the filtering and joining that I'm doing?
library(dplyr)
test <- tibble(
ID = c(1:10),
col1 = sample(c("Yes", "No", "Unknown"), 10, replace = TRUE),
col2 = sample(c("Yes", "No", "Unknown"), 10, replace = TRUE),
col3 = sample(c("Yes", "No", "Unknown"), 10, replace = TRUE)
)
left_join(test, test %>%
filter_at(vars(col1:col3), any_vars(. == "Yes")) %>%
mutate(any_symptoms = "Yes") %>%
select(ID, any_symptoms),
by = "ID"
) %>%
mutate(any_symptoms = recode(any_symptoms, .missing = "No"))
#> # A tibble: 10 x 5
#> ID col1 col2 col3 any_symptoms
#> <int> <chr> <chr> <chr> <chr>
#> 1 1 Unknown Unknown Unknown No
#> 2 2 Unknown No No No
#> 3 3 Yes Yes Unknown Yes
#> 4 4 No Unknown Unknown No
#> 5 5 No No Unknown No
#> 6 6 Unknown Yes Unknown Yes
#> 7 7 Yes Unknown Unknown Yes
#> 8 8 No No No No
#> 9 9 No Unknown Unknown No
#> 10 10 No No No No
Created on 2020-05-29 by the reprex package (v0.3.0)