Let's say I have this dataset:
library(data.table)
comb <- CJ(1:4, 2005:2008)
comb$var3 <- c(1, NA, 2, NA, 3, NA, 4, NA, 5, NA, 6, NA, 7, NA, 8, NA)
I want to fill the NAs according to the previous value. For instance, I want for Id 1 & year 2006 the value 1. For 2008 the value 2. The same for the other IDs. In the end the dataset should look like this:
library(data.table)
comb <- CJ(1:4, 2005:2008)
comb$var3 <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8)
How can I achieve it?