在data.table中使用带有.SD的na.locf函数(zoo包)

时间:2015-07-18 14:06:24

标签: r data.table zoo

我正在尝试填写所有NA,排除第1和第4列的前两个NA以及第2列和第3列的三个NA,最近的非NA值。这是我的数据和代码:

  hh<-structure(list(ka = c(NA, NA, 2, NA, NA, 3, NA, NA, NA, NA), 
        kb = c(NA, NA, NA, 2, NA, NA, 3, NA, NA, NA), gc = c(NA, 
        NA, NA, 3, NA, NA, 6, NA, NA, NA), hc = c(NA, NA, 8, NA, 
        NA, NA, 4, NA, NA, NA)), .Names = c("ka", "kb", "gc", "hc"
    ), row.names = c(NA, -10L), class = "data.frame")


library(zoo) #na.locf
library(data.table)

setDT(hh)[,`:=`(ka=c(NA,NA,na.locf(ka)),kb=c(NA,NA,NA,na.locf(kb)),gc=c(NA,NA,NA,na.locf(gc)),hc=c(NA,NA,na.locf(hc)))][]
    ka kb gc hc
 1: NA NA NA NA
 2: NA NA NA NA
 3:  2 NA NA  8
 4:  2  2  3  8
 5:  2  2  3  8
 6:  3  2  3  8
 7:  3  3  6  4
 8:  3  3  6  4
 9:  3  3  6  4
10:  3  3  6  4

但是,我正在寻找lapply.SD的使用,因为每种类型都有两列以上。这可能吗?

3 个答案:

答案 0 :(得分:7)

尝试

 setDT(hh)[, lapply(.SD, function(x) na.locf(x, na.rm=FALSE))]

或使用set

  for(j in seq_along(hh)){
    set(hh, i=NULL, j=j, value= na.locf(hh[[j]], na.rm=FALSE))
  }

答案 1 :(得分:0)

您可以使用development version 1.12.3中的setnafill

setnafill(hh, type = "locf")
hh
#    ka kb gc hc
#  1 NA NA NA NA
#  2 NA NA NA NA
#  3  2 NA NA  8
#  4  2  2  3  8
#  5  2  2  3  8
#  6  3  2  3  8
#  7  3  3  6  4
#  8  3  3  6  4
#  9  3  3  6  4
# 10  3  3  6  4

答案 2 :(得分:0)

您不需要lapply。足够了:

DT <- as.data.table(hh)
DT[, na.locf(.SD, na.rm = FALSE)]

给予:

    ka kb gc hc
 1: NA NA NA NA
 2: NA NA NA NA
 3:  2 NA NA  8
 4:  2  2  3  8
 5:  2  2  3  8
 6:  3  2  3  8
 7:  3  3  6  4
 8:  3  3  6  4
 9:  3  3  6  4
10:  3  3  6  4

这也将起作用:

DT[, lapply(.SD, na.locf0)]