简体   繁体   中英

How can I use rowSums with conditions to return binary value?

Say I have a data frame with a column for summed data. What is the most efficient way to return a binary 0 or 1 in a new column if any value in columns a, b, or c are NOT zero? rowSums is fine for a total, but I also need a simple indicator if anything differs from a value.

tt <- data.frame(a=c(0,-5,0,0), b=c(0,5,10,0), c=c(-5,0,0,0))
tt[, ncol(tt)+1] <- rowSums(tt)

This yields:

> tt
   a  b  c V4
1  0  0 -5 -5
2 -5  5  0  0
3  0 10 10 20
4  0  0  0  0

The fourth column is a simple sum of the data in the first three columns. How can I add a fifth column that returns a binary 1/0 value if any value differs from a criteria set on the first three columns?

For example, is there a simple way to return a 1 if any of a, b, or c are NOT 0?

as.numeric(rowSums(tt != 0) > 0)
# [1] 1 1 1 0

tt != 0 gives us a logical matrix telling us where there are values not equal to zero in tt .

When the sum of each row is greater than zero ( rowSums(tt != 0) > 0) , we know that at least one value in that row is not zero.

Then we convert the result to numeric ( as.numeric(.) ) and we've got a binary vector result.

We can use Reduce

+(Reduce(`|`, lapply(tt, `!=`, 0)))
#[1] 1 1 1 0

One could also use the good old apply loop:

+apply(tt != 0, 1, any)
#[1] 1 1 1 0

The argument tt != 0 is a logical matrix with entries stating whether the value is different from zero. Then apply() with margin 1 is used for a row-wise operation to check if any of the entries is true . The prefix + converts the logical output into numeric 0 or 1. It is a shorthand version of as.numeric() .

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