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
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]]
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.
Apparently Analagous Threads
- Create variables with common values for each group
- Using by() and stacking back sub-data frames to one data frame
- Using apply to generate matrix from rows?
- System is computationally singular and scale of covariates
- Importing csv file with character values into sqlite3 and subsequent problem in R / RSQLite