【发布时间】:2017-02-15 07:23:53
【问题描述】:
我有 2 个数据框,testx 和 testy
testx
testx <- structure(list(group = 1:2), .Names = "group", class = "data.frame", row.names = c(NA,
-2L))
暴躁
testy <- structure(list(group = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
time = c(1L, 3L, 4L, 1L, 4L, 5L, 1L, 5L, 7L), value = c(50L,
52L, 10L, 4L, 84L, 2L, 25L, 67L, 37L)), .Names = c("group",
"time", "value"), class = "data.frame", row.names = c(NA, -9L
))
基于this topic,我使用以下代码添加了缺失的时间值,效果很好。
data <- setDT(testy, key='time')[, .SD[J(min(time):max(time))], by = group]
现在,如果 group 的值出现在 testx 中,我只想添加这些缺失的时间值。在此示例中,我只想为与文件 testx 中的组值匹配的组添加缺失的时间值。
data <- setDT(testy, key='time')[,if(testy[group %in% testx[, group]]) .SD[J(min(time):max(time))], by = group]
我得到的错误是“选择了未定义的列”。我查看了here、here 和here,但我不明白为什么我的代码不起作用。我在大型数据集上这样做,为什么我更喜欢使用data.table。
【问题讨论】:
标签: r data.table