[R] Drop firms in unbalanced panel if not more than 5 observations in consecutive years for all variables

Stefan Grosse singularitaet at gmx.net
Thu Jul 22 12:28:44 CEST 2010


Am 22.07.2010 11:18, schrieb Christian Schoder:

> I use the plm package and work with firm-level data in a panel. I would
> like to eliminate all firms that do not fulfill the requirement of
> having an observation in every variable used for at least x consecutive
> years. 

There are probably more ellegant solutions but you could do something like:

test<-data.frame(year=c(2001,2001,2002,2001,2002,2003),id=c(1,2,2,3,3,3),x=runif(6))
test1<-aggregate(test,by=list(id=test$id),FUN=c("length","mean"))
test2<-merge(test,test1[,c("id","x")],by.x="id",by.y="id")
names(test2)<-c(names(test),"count")
testsub<-subset(test2,count>2)
testsub

hth
Stefan



More information about the R-help mailing list