[R] How to count rows with a condition

arun smartpink111 at yahoo.com
Thu Oct 18 02:42:08 CEST 2012


HI David,

I tried ur function:
set.seed(1)
dat1<-data.frame(ac_name=rep(c("HouseA","HouseB","HouseC","HouseD","HouseE"),times=c(8,5,4,6,3)),val=rnorm(26,15))
dat2<-within(dat1,{ac_name<-as.character(ac_name)})
dat2<-dat2[order(dat2[,1]),]

 dat2[ave(dat2$ac_name,dat2$ac_name,length)<=5,]
#Error in unique.default(x) : unique() applies only to vectors
#With "FUN" added
head(dat2[ave(dat2$ac_name,dat2$ac_name,FUN=length)<=5,])
#   ac_name      val
#9   HouseB 15.57578
#10  HouseB 14.69461
#11  HouseB 16.51178
#12  HouseB 15.38984
#13  HouseB 14.37876
#14  HouseC 12.78530
A.K.






----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: fxen3k <f.sehardt at gmail.com>
Cc: r-help at r-project.org
Sent: Wednesday, October 17, 2012 4:25 PM
Subject: Re: [R] How to count rows with a condition


On Oct 17, 2012, at 5:44 AM, fxen3k wrote:

> 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.
> 

data[ ave(data$ac_name, data$ac_name, length) <= 5, ]  # all with 5 or fewer entries

--
David Winsemius, MD
Alameda, CA, USA

______________________________________________
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