0

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]
Community
  • 1
  • 1
Jakob
  • 1,325
  • 15
  • 31
  • There is another approach that is not included in the duplicate question. `f <- approxfun(DT$x)` and then `DT[, y:= f(.I)]` – manotheshark Mar 06 '17 at 19:28
  • thanks for the help! In that case, I delete the question here? – Jakob Mar 06 '17 at 19:34
  • @PeterPan [No, please just leave your question as a sign-post](http://meta.stackoverflow.com/questions/265736/should-i-delete-my-question-if-it-is-marked-as-a-duplicate). – Henrik Mar 06 '17 at 19:37

0 Answers0