在“data.table”中尝试unlist
list
列时,每个list
元素可能属于不同类型,“data.table”会产生错误,停止执行:
library(data.table)
DT <- data.table(V1 = c("A", "B", "C"),
V2 = list(c(1, 2), NA, c(10, 20, 30)))
DT[, list(unlist(V2)), V1]
# Error in `[.data.table`(DT, , list(unlist(V2)), V1) :
# Column 1 of result for group 2 is type 'logical' but expecting
# type 'double'. Column types must be consistent for each group.
正如可以从错误中找出的那样,问题是NA
值不是预期的特定NA
类型(在本例中为NA_real_
)。< / p>
DT2 <- data.table(V1 = c("A", "B", "C"),
V2 = list(c(1, 2), NA_real_, c(10, 20, 30)))
DT2[, list(V2 = unlist(V2)), V1]
# V1 V2
# 1: A 1
# 2: A 2
# 3: B NA
# 4: C 10
# 5: C 20
# 6: C 30
问题: 是否有方便的方式继续进行unlist
- 即使warning
发出melt
当度量变量属于不同类型时?
DT3 <- data.table(V1 = c("A", "B", "C"), V2_1 = c(1, 2, 3),
V2_2 = NA, V2_3 = c(10, 20, 30))
melt(DT3, "V1")
# Warning message:
# In melt.data.table(DT3, "V1") :
# 'measure.vars' [V2_1, V2_2, V2_3] are not all of the same type. By order of
# hierarchy, the molten data value column will be of type 'double'. All measure
# variables not of type 'double' will be coerced to. Check DETAILS in
# ?melt.data.table for more on coercion.
我不是在寻找非data.table方法。例如,我已经知道来自“tidyr”的unnest()
会起作用(甚至不会发出警告):
tidyr::unnest(DT)
# V1 V2
# 1 A 1
# 2 A 2
# 3 B NA
# 4 C 10
# 5 C 20
# 6 C 30