[R] Using apply for logical conditions

Alastair alastair.andrew at gmail.com
Mon Aug 2 22:35:14 CEST 2010


Hi,

I've got some boolean data in a data.frame in the form:
      X    Y    Z    A   B   C
[1]  T     T    F    T   F   F
[2]  F     T    T    F   F   F
.
.
.


What I want to do is create a new column which is the logical disjunction of
several of the columns.
Just like:

new.column <- data$X | data$Y | data$Z

However I don't want to hard code the particular columns into the expression
like that. I've tried using apply row wise with `|` as the function:

columns <- c(X,Y,Z)
apply(data[,columns], 1,`|`)

This doesn't seem to do what I would have expected, does anyone have any
advice how to use the the apply or similar function to perform a boolean
operation on each row (and a specific subset of the columns) in a data
frame? 

Thanks,
Alastair


-- 
View this message in context: http://r.789695.n4.nabble.com/Using-apply-for-logical-conditions-tp2310929p2310929.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list