[R] tapply and any
Peter Alspach
PAlspach at hortresearch.co.nz
Wed Nov 19 19:59:20 CET 2008
Stephan
Try:
tapply(y[,2], y[,1], function(x) any(x==5))
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Stephan Lindner
> Sent: Thursday, 20 November 2008 7:48 a.m.
> To: r-help at stat.math.ethz.ch
> Subject: [R] tapply and any
>
>
> Dear all,
>
>
> A quick question which I somehow cannto figure out: I want to
> apply the function "any" to subsets of a dataset in order to
> create a vector with TRUE/FALSE values, depending on whether
> a subset has the number 5. I.e.,
>
> y <- matrix(c(1,2,3,3,4,5,5,6,6,7,5,1,1,3,5,NA,5,1,1,3),ncol=2)
>
> > y
> [,1] [,2]
> [1,] 1 5
> [2,] 2 1
> [3,] 3 1
> [4,] 3 3
> [5,] 4 5
> [6,] 5 NA
> [7,] 5 5
> [8,] 6 1
> [9,] 6 1
> [10,] 7 3
>
> ## y[,1] is the index, y[,2] the object for which I apply the
> function.
>
>
> > tapply(y[,2],y[,1],any,5)
> 1 2 3 4 5 6 7
> TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>
> > tapply(y[,2],y[,1],function(i,x,y) any(x[i]==y),x=y[,2],y=5)
> 1 2 3 4 5 6 7
> TRUE TRUE TRUE TRUE TRUE TRUE FALSE
>
> I want to obtain:
>
> 1 2 3 4 5 6 7
> TRUE FALSE FALSE TRUE TRUE FALSE FALSE
>
>
>
> Thanks!
>
>
> Stephan
>
>
>
>
> --
> -----------------------
> Stephan Lindner
> University of Michigan
>
> ______________________________________________
> 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.
>
The contents of this e-mail are privileged and/or confidential to the named
recipient and are not to be used by any other person and/or organisation.
If you have received this e-mail in error, please notify the sender and delete
all material pertaining to this e-mail.
More information about the R-help
mailing list