Subsetting a unbalanced panel dataset to have at least 2 consecutive observations in R

panel-data, r, subset

Solution

My (hackish) way to do it would be:

is.consecutive = duplicated(rbind(dt,transform(dt, year=year+1), 
                                     transform(dt, year=year-1)),
                            fromLast=TRUE)[1:nrow(dt)]

`is.consecutive` contains a vector of booleans of the observations to be retained. For your example, this vector would be: `TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE`

Finally, you can easily use this vector to subset your data.frame, e.g. with:

dt[is.consecutive,]

Problem

I have an unbalanced panel dataset in R. The following will serve as an example: ``` dt <- data.frame(name= rep(c("A", "B", "C"), c(3,2,3)), year=c(2001:2003,2000,2002,2000:2001,2003)) > dt name year 1 A 2001 2 A 2002 3 A 2003 4 B 2000 5 B 2002 6 C 2000 7 C 2001 8 C 2003 ``` Now, I need to have at least 2 consecutive `year` observations for each `name`. Hence, I would like to remove row 4, 5, and 8. How do I best do that in R? EDIT: Thanks to the comment below, I can make a bit clearer. If I had an extra observation (row 9) with `name`=`C` and `year`=`2004`, I would want to keep both row 8 and 9 along with the others.

Original source