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
?table Assuming that your data frame is named as x. tbl <- table(x) tbl[tbl[,"Bad"]<3,] HTH -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sean Liang Sent: Thursday, March 11, 2004 7:09 AM 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 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
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