Create a new variable using mutate in boolean conditions for many variables - mutate?
Can I mutate
create new variables based on boolean conditions applied to variables?
In particular, 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 what I would like to do, but essentially eliminating all boolean comparisons (because I have a lot to do). 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 better procedure for this task to avoid the very long set of ifelse conditionals? I found a similar question here , but I want to apply one boolean comparison, not write each one. I couldn't figure out how to make the following code using data.table
. Any help would be greatly appreciated!
source to share
As always, the optimal answer will depend on the specific question.
In this case, for example, you can use pmax()
:
dat$anyABCD <- with(dat, pmax(a, b, c, d) == 1)
head(dat)
a b c d anyABCD
1 0 0 0 0 FALSE
2 0 0 0 0 FALSE
3 0 0 0 0 FALSE
4 0 0 0 0 FALSE
5 0 0 0 0 FALSE
6 0 0 0 1 TRUE
You can also use a function apply
like:
dat$anyABCD <- apply(dat[, 1:4], 1, function(x)max(x) == 1)
head(dat)
a b c d anyABCD
1 0 0 0 0 FALSE
2 0 0 0 0 FALSE
3 0 0 0 0 FALSE
4 0 0 0 0 FALSE
5 0 0 0 0 FALSE
6 0 0 0 1 TRUE
And if you are sure that your data is binary, you can use rowSums()
:
dat$anyABCD <- rowSums(dat[, 1:4] >= 1)
head(dat)
a b c d anyABCD
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
6 0 0 0 1 1
source to share