[R] subsetting by rows

Patrick Burns pburns at pburns.seanet.com
Wed Aug 31 20:22:46 CEST 2011


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')



More information about the R-help mailing list