rbindlist:NA 到日期时间列

rbindlist: NA into date-time column

我正在尝试构建一个函数以将一行 NA 插入到 data.table 中。我以这种方式使用 rbindlist 来完成它,其中 xdata.table:

rbindlist(
  list(
    x,
    as.list(rep(NA, ncol(x)))
  )
)

我遇到了 POSIXct 列无法与 NA 值绑定的问题,如:

x <- data.table(
  a=c(1,2),
  t=c(Sys.time(), Sys.time()+100)
)

rbindlist(
  list(
    x,
    as.list(rep(NA, ncol(x)))
  )
)

这导致我出现以下错误:

    Error in rbindlist(list(x, as.list(rep(NA, ncol(x))))) : 
  Class attributes at column 2 of input list at position 2 does not match with column 2 of input list at position 1. Coercion of objects of class 'factor' alone is handled internally by rbind/rbindlist at the moment.

所以它无法绑定 NA 我提供的 POSIXctx$t

问题:如何将所有 NA 值的单行 data.table 绑定到具有 POSIXct 类型列的 data.table ?

我试过以下方法,结果是同样的错误:

rbindlist(
  list(
    x,
    as.list(c(NA, as.POSIXct(NA)))
  )
)

感兴趣:在原始 data.table

上调用以下内容
x[2, t:=NA]
x[2, a:=NA]

Returns:

    a                   t
1:  1 2019-04-04 12:38:57
2: NA                <NA>

你可以做到

library(data.table)
x <- data.table(
  a=c(1,2),
  t=c(Sys.time(), Sys.time()+100)
)

x[c(1:.N, NA)]

#     a                   t
# 1:  1 2019-04-04 13:01:34
# 2:  2 2019-04-04 13:03:14
# 3: NA                <NA>

# or

rbind(x, x[NA])

在 base R 中,您将对后者使用 NA_integer_,但为了方便起见,data.table 以相同的方式对待 NA。您可以看到特殊处理,例如 x[(NA)]。这记录在 vignette("datatable-faq"):

2.17 What are the smaller syntax differences between data.frame and data.table

[...]

DT[NA] returns 1 row of NA, but DF[NA] returns an entire copy of DF containing NA throughout. The symbol NA is type logical in R and is therefore recycled by [.data.frame. The user's intention was probably DF[NA_integer_]. [.data.table diverts to this probable intention automatically, for convenience.

你也可以这样做-

library(data.table)
> a <- copy(x)[1]
> a[a] <- NA
> rbind(x,a)
    a                   t
1:  1 2019-04-04 12:54:47
2:  2 2019-04-04 12:56:27
3: NA                <NA>