【发布时间】:2016-12-21 12:08:20
【问题描述】:
我现在找不到重复的。
我的问题如下:
我有两个data.tables。一个有两列(featurea、count),另一个有三列(featureb、featurec、count)。我想乘以(?),以便我有一个新的data.table 具有所有可能性。诀窍是这些功能不匹配,因此merge 解决方案可能无法解决问题。
MRE如下:
# two columns
DT1 <- data.table(featurea =c("type1","type2"), count = c(2,3))
# featurea count
#1: type1 2
#2: type2 3
#three columns
DT2 <- data.table(origin =c("house","park","park"), color =c("red","blue","red"),count =c(2,1,2))
# origin color count
#1: house red 2
#2: park blue 1
#3: park red 2
在这种情况下,我的预期结果是data.table,如下所示:
> DT3
origin color featurea total
1: house red type1 4
2: house red type2 6
3: park blue type1 2
4: park blue type2 3
5: park red type1 4
6: park red type2 6
【问题讨论】:
-
DT2[, .(featurea = DT1[["featurea"]], count = count * DT1[["count"]]), by = .(origin, color)]是否足够高效? -
@Roland 似乎是这样,这听起来是最好的答案,所以你应该这样发布它
标签: r data.table