The zoo
package has several methods for automatically filling in missing time-series data.
a <- matrix(as.numeric(as.matrix(sweden[, 5:52])), nrow = 48, byrow = T) a <- zooreg(a, frequency = 12, start = 2005) ## replacing each ‘NA’ with the most recent non-‘NA’ prior to it. a <- na.locf(a, na.rm = F) a <- na.locf(a, na.rm = F, fromLast = T) ## turn the data part of a into factors again coredata(a) <- matrix(factor(coredata(a)), nrow = 48, ncol = 1929)