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.
`|` is a binary operator which is why the apply will not work. See help("Reduce") For example, set.seed(1) data <- data.frame(A = runif(10) > 0.5, B = runif(10) > 0.5, C = runif(10) > 0.5) Reduce(`|`, data) # [1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE Hope this helps. Allan On 02/08/10 21:35, Alastair wrote:> 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 > > >
Alastair wrote:> 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,`|`) >Please provide *reproducible* examples. I cannot run any of your code since you don't give us the objects X, Y, or Z. An easy way to do this is to use ?dput on the objects we need to run your code, e.g., your data.frame. Does this do what you want? df1 <- data.frame(x = sample(c(TRUE, FALSE), 10, replace = TRUE), y = sample(c(TRUE, FALSE), 10, replace = TRUE), z = sample(c(TRUE, FALSE), 10, replace = TRUE)) columns <- c("x", "y", "z") apply(df1[columns], 1, any)
Reduce() is much nicer, but I usually use rowSums(A) > 0 for 'or', and rowSums(A) == ncols for 'and'. Which works slightly faster. I noticed, though, that Reduce() doesn't work on matrices. Is there an alternative for matrices, or do you have to convert the matrix first to a data.frame, and then use Reduce? -- View this message in context: http://r.789695.n4.nabble.com/Using-apply-for-logical-conditions-tp2310929p2310991.html Sent from the R help mailing list archive at Nabble.com.