Re: [R] Using apply for logical conditions

2010-08-02 Thread Alastair
Wow, Thanks for all the excellent (and fast) responses. That's really helped. Sorry I didn't supply a cut and paste-able example (noted for future reference) but your examples caught the essence of my problem. I ended up opting for the apply any solution. But I'll bear the Reduce function in m

Re: [R] Using apply for logical conditions

2010-08-02 Thread Bert Gunter
Just for fun, here are another couple of versions that work for data frames. For Reduce with "|" do.call(pmax,c(mydata,na.rm=TRUE)) >0 and for "&" do.call(pmin,c(mydata,na.rm=TRUE)) >0  Cheers, Bert Gunter Genentech Nonclinical Biostatistics On Mon, Aug 2, 2010 at 2:28 PM, Joshua Wiley wr

Re: [R] Using apply for logical conditions

2010-08-02 Thread Michael Lachmann
Reduce() is really amazingly fast! Even with a much larger number of columns, it is still in the same ballpark (and much more readable): > boolean <- c(TRUE, rep(FALSE,10^3)) > a<-matrix(sample(boolean, 10^7, replace = TRUE),10^4,10^3) > b<-data.frame(a) > system.time({opt4 <- rowSums(a, na.rm =

Re: [R] Using apply for logical conditions

2010-08-02 Thread Joshua Wiley
On Mon, Aug 2, 2010 at 2:08 PM, Michael Lachmann wrote: > > Reduce() is much nicer, but I usually use > > rowSums(A) > 0 for 'or', and > rowSums(A) == ncols for 'and'. > > Which works slightly faster. For the sake of my own curiosity, I compared several of these options, but in case others are in

Re: [R] Using apply for logical conditions

2010-08-02 Thread Bert Gunter
Yes, you must do the conversion. The reason is that Reduce requires its argument x, to be a vector; and a matrix is seen a vector obtained by columnwise concatenation. e.g. > Reduce("+",matrix(1:6,nr=3)) [1] 21 > Reduce("+",1:6) [1] 21 The data frame is seen as a list with elements the columns of

Re: [R] Using apply for logical conditions

2010-08-02 Thread Michael Lachmann
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 the

Re: [R] Using apply for logical conditions

2010-08-02 Thread Joshua Wiley
In addition to Reduce(), you can take a look at ?any for '|' and ?all for '&'. Josh On Mon, Aug 2, 2010 at 1:43 PM, Allan Engelhardt wrote: > `|` 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)

Re: [R] Using apply for logical conditions

2010-08-02 Thread Erik Iverson
Alastair wrote: Hi, I've got some boolean data in a data.frame in the form: XYZA B C [1] T TFT F F [2] F TTF 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.

Re: [R] Using apply for logical conditions

2010-08-02 Thread Allan Engelhardt
`|` 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.

[R] Using apply for logical conditions

2010-08-02 Thread Alastair
Hi, I've got some boolean data in a data.frame in the form: XYZA B C [1] T TFT F F [2] F TTF 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 |