我現在找不到重複。乘以兩個data.tables,保留所有可能
我的問題是這樣的:
我有兩個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」], 計數=計數* DT1 [」 count「]]),by =。(origin,color)]'效率足夠高嗎? – Roland
@羅蘭似乎是這樣,這聽起來是最好的答案,所以你應該這樣發佈 – Tensibai