【发布时间】:2017-02-23 03:22:47
【问题描述】:
我在 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。
【问题讨论】:
-
您确定要将
c2和c3列为列表吗?在您的示例中,您似乎只在其中保留了一个值。 -
是的,我没有发布我的应用程序代码,因为它更混乱,但它们是具有多个值的属性列表。
标签: r data.table