简体   繁体   中英

Create new variable using mutate on logical conditions across many variables - mutate?

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!

As always, the optimal answer is going to 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 an apply function, for example:

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 quite certain 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM