[R] How to count rows with a condition

William Dunlap wdunlap at tibco.com
Wed Oct 17 22:18:06 CEST 2012

One way is:
  ac_name_count <- ave(integer(nrow(data)), data[["ac_name"]], FUN=length)
  data[ac_name_count <= 5, ,drop=FALSE] # rows whose ac_name entry is rare
  data[ac_name_count > 5, ,drop=FALSE]  # rows whose ac_name entry is common
  ac_name_seqno <- ave(integer(nrow(data)), data[["ac_name"]], FUN=seq_along)
to assign a within-group sequence number so you can pick out the first or last
n items in a group for the big groups.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of fxen3k
> Sent: Wednesday, October 17, 2012 5:45 AM
> To: r-help at r-project.org
> Subject: [R] How to count rows with a condition
> Hi,
> I have a dataset called "data". There is one row called "ac_name". Some
> names in this column appear very often, some less.
> What I want is to filter this dataset with the following condition:
> Exclude the names, which appear more than five times. (example: House A
> appears 8 times ==> exclude it; House B appears 5 times ==> include it etc.)
> In the end, I want to have the old "data" dataset excluding the rows with
> the above mentioned condition and another list with all the names which have
> been excluded.
> I think for one of the professionals amongst you this is pretty easy to
> solve. ;-)
> Thanks dudes!
> Cheerio,
> Felix
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-count-rows-with-
> a-condition-tp4646454.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> 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.

More information about the R-help mailing list