[R] Panel Data--filling in missing dates in a span only

Steven Archambault archstevej at gmail.com
Tue Mar 10 23:53:24 CET 2015

Hi folks,

I have this panel data (below), with observations missing in each of the panels. I want to fill in years for the missing data, but only those years within the span of the existing data. For instance, BC-0002 needs on year, 1995. I do not want any years after the last observation.

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("BC-0002", 
"BC-0003", "BC-0004"), class = "factor"), Date = c(1989L, 1990L, 
1991L, 1992L, 1993L, 1994L, 1996L, 1989L, 1990L, 1991L, 1992L, 
1993L, 1994L, 1996L, 1995L, 1996L, 1997L, 1998L, 2000L, 1994L, 
1993L, 1999L, 1998L), DepthtoWater_bgs = c(317.85, 317.25, 321.25, 
312.31, 313.01, 330.41, 321.01, 166.58, 167.55, 168.65, 168.95, 
169.25, 168.85, 169.75, 260.6, 261.65, 262.15, 265.45, 266.15, 
265.25, 265.05, 266.95, 267.75)), .Names = c("ID", "Date", "DepthtoWater_bgs"
), class = "data.frame", row.names = c(NA, -23L))

I have been using this code to expand the entire panels, but it is not what exactly what I want.

fexp <- expand.grid(ID=unique(wells$ID), Date=unique(wells$Date))
merge(fexp, wells, all=TRUE) 

Any help would be much appreciated!


	[[alternative HTML version deleted]]

More information about the R-help mailing list