Can mutate
create new variables based on logical conditions applied across variables?
More specifically, consider the following data.
set.seed(1234)
val <- c(rep(0, 20), 1)
a <- sample(val, 50, replace=T)
b <- sample(val, 50, replace=T)
c <- sample(val, 50, replace=T)
d <- sample(val, 50, replace=T)
dat <- data.frame(a,b,c,d)
Here is some pseudo-code that gets at what I would like to do, but essentially removing all the logical comparisons (because I have a lot to make). Obviously this is not functional code.
new.dat <- mutate(dat, anyABCD == ifelse(A or B or C or D == 1, 1, 0))
Is there a more efficient procedure for handling this task to avoid a very long set of ifelse conditionals? I found a similar question here but I want to apply one single logical comparison rather than writing each one out. I could not figure out how to make the subsequent code work using data.table
. Any help would be greatly appreciated!