Try this:

sapply(tapply(y[,2],na.omit(y[,1]), `==`, 5), any)

On Wed, Nov 19, 2008 at 4:47 PM, Stephan Lindner <lindners@umich.edu> wrote:

>
> 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@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

