[R] tapply and any

Stephan Lindner lindners at umich.edu
Wed Nov 19 19:47:37 CET 2008


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



More information about the R-help mailing list