[R] data frame filtration

Gabor Grothendieck ggrothendieck at myway.com
Thu Mar 11 02:53:06 CET 2004



Suppose the data frame is called fruit:

do.call("rbind", 
 by(fruit, fruit$Fruit, function(x)if (sum(x$Condition=="Bad")<3) x))

by splits the data frame into groups and then applies the 
indicated function to each group.  If the condition is met then
that group is returned; otherwise, NULL is returned since there
is no else leg to the if.  rbind then binds the groups back into
a data frame.

---
Date:   Wed, 10 Mar 2004 17:09:23 -0500 
From:   Sean Liang <SLiang at wyeth.com>
To:   <R-help at stat.math.ethz.ch> 
Subject:   [R] data frame filtration 

 
Hi, R-help:

I am a new user of R and am very pleased with R's features. Here I have
one question regarding data frame manipulation. I have a data frame look
like this:

Fruit Condition
1 Orange Good
2 Orange Bad
3 Orange Good
4 Orange Good
5 Orange Bad
6 Apple Good
7 Apple Bad
8 Apple Good
9 Apple Good
10 Apple Bad
11 Apple Good
12 Apple Bad
13 Mango Good
14 Mango Good
15 Mango Bad

and I like to remove fruit group(s) with three or more "Bad" pieces. In
this case, I want to remove Apple group. Is there an easy way to count
the "Good" and "Bad" in each group then remove the ones that meet the
criteria? 

Thanks for your help.

Sean




More information about the R-help mailing list