Jennifer
Jennifer

Reputation: 17

How do you use mutate_at with case_when for different columns to fill different values?

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.

  1. QueryText
  2. QueryStatus

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

Answers (1)

Ronak Shah
Ronak Shah

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

Related Questions