Reputation: 17
I have a data frame (demo_df) with multiple columns and I am trying to append two new columns based on certain conditions. These two columns will be populated with different values if the conditions are met. I am using case_when inside mutate_at to mutate two variables.
How do I do this? So far I have the following code.
queries <- demo_df%>%
mutate_at(vars(c(QueryText, QueryStatus)) = case_when(
VISIT == "SCRN" & nles <=3 ~ {"Respond","High"},
VISIT == "D1" & nles >3 ~ {"Ignore","Low"}))
Upvotes: 0
Views: 152
Reputation: 388907
You cannot assign multiple columns in one case_when
statement. Try :
library(dplyr)
queries <- demo_df %>%
mutate(QueryText = case_when(
VISIT == "SCRN" & nles <=3 ~ "Respond"},
VISIT == "D1" & nles >3 ~ "Ignore"),
QueryStatus = case_when(QueryText == 'Respond' ~ 'High',
QueryText == 'Ignore' ~ 'Low'))
Upvotes: 1