I have panel data in R. data frame containing years from 1989 to 2008 for armed conflicts in different countries. However, only observations are included for countries that have experienced armed conflict in a given year.
The data set is similar to this:
df <- data.frame(c("1989","1993","1998", "1990","1995","1997"), c(rep(c(750, 135), c(3,3))), c(rep(1,6))) names(df)<-c("year","countrycode","conflict") print(df) year countrycode conflict 1 1989 750 1 2 1993 750 1 3 1998 750 1 4 1990 135 1 5 1995 135 1 6 1997 135 1
Now I want to fill in the gaps in the panel data, BUT ONLY the gaps that do not exceed three years. For example, I want to add lines between lines 1 and 2 and between lines 5 and 7 (spacing 3 and 1 year, respectively), but neither between lines 2 and 3, nor between lines 4 and 5 (spaces are 4 years each). After this The above data frame will look like this:
> df2 <- data.frame(c("1989","1990","1991","1992","1993","1998", + "1990","1995","1996","1997"), + c(rep(c(750, 135), c(6,4))), c(1,0,0,0,1,1,1,1,0,1)) > names(df2) <- c("year","countrycode","conflict") > print(df2) year countrycode conflict 1 1989 750 1 2 1990 750 0 3 1991 750 0 4 1992 750 0 5 1993 750 1 6 1998 750 1 7 1990 135 1 8 1995 135 1 9 1996 135 0 10 1997 135 1
I looked at the plm package (see here ), but could not find an answer. In addition, I am relatively new to R, so I will be happy for any hint.