Dear all, I would like to know how to subset a data.frame by rows. Example: Probesets 34884 34888 34892 1 100009676_at A A A 2 10001_at P P P 3 10002_at A A A 4 10003_at A A A 5 100048912_at P A A For this data.frame I want to retrieve only the rows where at least one ´P´ is found. So in this example it would be rows 2 and 5. Best regards and thanks in advance [[alternative HTML version deleted]]
Suppose your data is called df. df[rowSums(df == "P")>0,] In short, this tests each element for equality for "P", sums the number of "P"s found and subsets when that number is >0. Michael On Wed, Aug 31, 2011 at 1:28 PM, Joao Fadista <Joao.Fadista@med.lu.se>wrote:> Dear all, > > I would like to know how to subset a data.frame by rows. > > Example: > > Probesets 34884 34888 34892 > 1 100009676_at A A A > 2 10001_at P P P > 3 10002_at A A A > 4 10003_at A A A > 5 100048912_at P A A > > For this data.frame I want to retrieve only the rows where at least one ´P´ > is found. So in this example it would be rows 2 and 5. > > Best regards and thanks in advance > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >[[alternative HTML version deleted]]
If your data frame is called 'df', then something like the following should work: df[ apply(as.matrix(df[,-1]), 1, function(x) any(x == "P")), ] This creates a logical vector as long as the number of rows. As Bill Dunlap recently noted, 'apply' really wants a matrix and not a data frame. On 31/08/2011 18:28, Joao Fadista wrote:> Dear all, > > I would like to know how to subset a data.frame by rows. > > Example: > > Probesets 34884 34888 34892 > 1 100009676_at A A A > 2 10001_at P P P > 3 10002_at A A A > 4 10003_at A A A > 5 100048912_at P A A > > For this data.frame I want to retrieve only the rows where at least one ?P? is found. So in this example it would be rows 2 and 5. > > Best regards and thanks in advance > > > [[alternative HTML version deleted]] > > > > > ______________________________________________ > 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.-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
Hi Joao, Here is one way (d is your data set): d[apply(d == 'P', 1, any),] HTH, Jorge On Wed, Aug 31, 2011 at 1:28 PM, Joao Fadista <> wrote:> Dear all, > > I would like to know how to subset a data.frame by rows. > > Example: > > Probesets 34884 34888 34892 > 1 100009676_at A A A > 2 10001_at P P P > 3 10002_at A A A > 4 10003_at A A A > 5 100048912_at P A A > > For this data.frame I want to retrieve only the rows where at least one ´P´ > is found. So in this example it would be rows 2 and 5. > > Best regards and thanks in advance > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >[[alternative HTML version deleted]]