[R] subset dataframe by number of rows of equal values

Cecilia Carmo cecilia.carmo at ua.pt
Sun May 24 19:46:09 CEST 2009


Hi R helpers!

I have the following dataframe «choose»
choose<-data.frame(firm=c(1,1,2,2,2,2,3,3,4,4,4,4,4,4), 
year=c(2000,2001,2000,2001,2002,2003,2000,2003,2001,2002,2003,2004,2005,2006),code=c(10,10,11,11,11,11,12,12,13,13,13,13,13,13))
choose

I want to subset it to obtain another one with those 
observations for which there more than 2 observations in 
the column «code». So I want a dataframe «chosen» like 
this:
chosen<-data.frame(firm=c(2,2,2,2,4,4,4,4,4,4),year=c(2000,2001,2002,2003,2001,2002,2003,2004,2005,2006),code=c(11,11,11,11,13,13,13,13,13,13))
chosen

I’ve tried split() and then nrow() but I got nothing.

Could anyone help me with this?

Thanks
Cecília (Universidade de Aveiro – Portugal)




More information about the R-help mailing list