2

I'd like to fill NAs with either forward or backward value filling with data.table in R. I saw a few posts explaining how to do it, but still have some trouble for multiple columns.

Say, I have a data table DT with three columns ("a", "b", "c")

I tried the following code but did not work

var.names <- c("a", "b", "c")
DT[, var.names := na.locf(list(var.names)), with = FALSE]

or

DT[, var.names, roll = TRUE, with = FALSE]
athlonshi
  • 1,711
  • 1
  • 19
  • 23
  • 5
    `DT[, var.names := lapply(.SD, na.locf), .SDcols = var.names]`? – Roland Jul 28 '15 at 14:47
  • Or this one: http://stackoverflow.com/questions/31491976/use-of-na-locf-function-zoo-package-with-sd-in-data-table/ – MichaelChirico Jul 29 '15 at 18:47
  • Thanks for the help. I read those posts, and my question is actually how to apply it to multiple columns by referring the column names. I think using lapply definitely is the solution. – athlonshi Jul 31 '15 at 13:30
  • Also http://stackoverflow.com/questions/31491976/ is quite helpful – athlonshi Jul 31 '15 at 13:54

0 Answers0