Cecilia Carmo
2009-May-24 17:46 UTC
[R] subset dataframe by number of rows of equal values
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)
Here is one way of doing it:> moreThan <- ave(choose$code, choose$code, FUN=length) > moreThan[1] 2 2 4 4 4 4 2 2 6 6 6 6 6 6> choose[moreThan > 2,]firm year code 3 2 2000 11 4 2 2001 11 5 2 2002 11 6 2 2003 11 9 4 2001 13 10 4 2002 13 11 4 2003 13 12 4 2004 13 13 4 2005 13 14 4 2006 13>On Sun, May 24, 2009 at 1:46 PM, Cecilia Carmo <cecilia.carmo@ua.pt> wrote:> 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) > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]