r - 在 R 的 data.table 中添加一个空列表作为值

标签 r data.table

我在 R 中有一个 data.table,其中一列对应于一个字符串列表,如下所示:

DT <- data.table(c1 = 1:3, 
                 c2 = as.list(c("bob",NA,"mary")), 
                 c3 = as.list(c(NA,"joe",NA)))

我想用一个空列表替换 NA 值,因为我稍后连接列 c2 和 c3,使用:

DT[, combined := list(list(unlist(union(c2,c3)))), by=c1]

这给了我

DT$combined
   [[1]] bob,NA
   [[2]] NA,joe
   [[3]] mary,NA

而不是想要的

DT$combined
   [[1]] bob
   [[2]] joe
   [[3]] mary

我可以通过将 NA 转换为空列表来获得所需的结果,这就是我的问题所在:如何以优雅的方式做到这一点?

我可以使用数据框语法摆脱 NA:

DT$c2[is.na(DT$c2)] <- list(list())

但是,因为我正在使用数据表,而且它们应该比那更好,所以我想做类似的事情

set(DT, DT[,.I[is.na(c2)]], "c2", value= list(list()))

R 向其吐出以下错误:

Error in set(DT, DT[, .I[is.na(c2)]], "c2", value = list(list())) : 
RHS of assignment to existing column 'c2' is zero length but not   NULL. 
If you intend to delete the column use NULL. Otherwise, the RHS must have length > 0; 
e.g., NA_integer_. If you are trying to change the column type to be an empty list column then, 
as with all column type changes, provide a full length RHS vector such as 
vector('list',nrow(DT)); i.e., 'plonk' in the new column.

我只是在寻找一种更好的方式来使用 data.tables。

最佳答案

向您的 list(list()) 添加显式 NULL:

DT[is.na(c2), c2 := .(list(NULL))]

# or loop over the relevant columns
for (col in c('c2', 'c3')) DT[is.na(get(col)), (col) := .(list(NULL))]

关于r - 在 R 的 data.table 中添加一个空列表作为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40030541/

相关文章:

r - 为通过引用替换的 data.tables 编写高效函数

r - 贴标机不使用 facet_wrap 应用标签并返回 NA

r - 通过连接点(lon-lat)在 R 中绘制的谷歌地图上查找位置区域

json - 使用R从JSON读取后如何避免循环列表

r - 按特定值顺序对数据表进行排序

r - 在R中的data.table中创建分组 "order"变量

r - 数据框和 is.nan()

r - 需要使用 dplyr 对多列变量的数据行求和

r - 如何获得后续观察(国家年)之间的值(value)差异?

r - 在 data.table 中使用 ifelse 选择每组一行