[R] Using apply for logical conditions
Bert Gunter
gunter.berton at gene.com
Mon Aug 2 23:22:19 CEST 2010
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 the
frame. Whence one concludes that the f argument must be vectorized for
the Reduce to work on the columns of the data frame as you expect.
e.g.
> Reduce(min,data.frame(a=1:3,b=4:6))
[1] 1
but
> Reduce(pmin,data.frame(a=1:3,b=4:6))
[1] 1 2 3
Cheers,
Bert Gunter
Genentech Nonclinical Biostatistics
On Mon, Aug 2, 2010 at 2:08 PM, Michael Lachmann <lachmann at eva.mpg.de> wrote:
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list