Say I have a data table which has data gaps and I want to fill these simply linearily using the last available and the next available data point:
DT = data.table(time=as.yearmon(rep(c("Jan03","Feb03", "Mar03", "Apr03", "May03"),2),"%b%y"),
x=c(4,7,6,4,5,5,8,12,6,4),
index=rep(c("a","b"),each=5))
DT[c(1,3,4,7,8,10),x:=NA]
and I want to fill the gaps. The goal would look like:
DT[,y:=c(NA,7,6.333,5.666,5,5,5.333,5.666,6,NA)]
So would be done by=index
. The answers I find on stack overflow are focused on daily variables without NAs (see here) which seems to be a different problem. I was thinking along the lines of working with a is.na()
function but I have not clue how to get further than that:
DT[,x:=ifelse(is.na(x),shift(x,1),x),by=index]