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
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
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 =
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
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
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
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)
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.
`|` 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.
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 |
10 matches
Mail list logo