Minimal example: A small dataframe with 4 columns and a variable that holds the name of a new column I want to create. The new column is TRUE if responses to more than a certain number of questions exceed a threshold, and is FALSE otherwise
df1 <- data.frame(ID = LETTERS[1:5],
Q1 = sample(0:10, 5, replace=T),
Q2 = sample(0:10, 5, replace=T)
Q3 = sample(0:10, 5, replace=T)
Q4 = sample(0:10, 5, replace=T)
)
This gives me my dataframe with responses to the various questions:
> df1
ID Q1 Q2 Q3 Q4
1 A 2 4 5 0
2 B 9 6 6 3
3 C 5 5 3 2
4 D 0 5 3 10
5 E 7 5 6 7
I also define the following constants:
QUESTIONS <- c("Q1”, “Q2”, “Q3”, “Q4")
MY_NEW_COL <- "New_Col"
THESHOLD1 <- 5
THESHOLD2 <- 2
I want to add a new column named New_Col that is TRUE if more than THRESHOLD2 columns have a value in excess of THRESHOLD1. I can get this to work in a clumsy, but obvious way:
df1 %>%
mutate(!!MY_NEW_COL := ( (Q1 > THREHOLD1) + (Q2> THREHOLD1) +
(Q3 > THREHOLD1) + (Q4> THREHOLD1) ) > THRESHOLD2)
This gives the right answer:
ID Q1 Q2 Q3 Q4 New_Col
1 A 2 4 5 0 FALSE
2 B 9 6 6 3 TRUE
3 C 5 5 3 2 FALSE
4 D 0 5 3 10 FALSE
5 E 7 5 6 7 TRUE
But I would like to systematize this up as there are 17 questions in all. My code, which I show below, gives the wrong answer
df1 %>%
mutate(!!MY_NEW_COL := sum(all_of(QUESTIONS) > THRESHOLD1)) > THRESHOLD2)
ID Q1 Q2 Q3 Q4 New_Col
1 A 2 4 5 0 TRUE
2 B 9 6 6 3 TRUE
3 C 5 5 3 2 TRUE
4 D 0 5 3 10 TRUE
5 E 7 5 6 7 TRUE
What am I doing wring, and how can I fix this?
Many thanks in advance
Thomas Philips