我有两个data.tables,一个有另一个的行/列子集。我想在较小的表中为每个非零值增加较大data.table的值1:
DT1 <- as.data.table(matrix(c(0, 1, 2, 3), nrow=2, ncol=2,
dimnames=list(c("a", "b"), c("a", "b"))), keep=T)
DT2 <- as.data.table(matrix(c(0, 0, 1, 2, 2, 1, 1, 0, 3), nrow=3, ncol=3,
dimnames=list(c("a", "b", "c"), c("a", "b", "c"))), keep=T)
DT1
# rn a b
#1: a 0 2
#2: b 1 3
DT2
# rn a b c
#1: a 0 2 1
#2: b 0 2 0
#3: c 1 1 3
我想在DT2中增加值,以便我得到
# rn a b c
#1: a 0 3 1
#2: b 1 3 0
#3: c 1 1 3
(这与我之前关于添加DT1和DT2的问题相似:Adding values in two data.tables ......我需要同时做两件事:))
答案 0 :(得分:5)
另一种方式:
su: uid xxxx not allowed
这应该如何:
require(data.table) # v1.9.6+
xcols = c("a", "b")
icols = paste0("i.", xcols) # "i.*" to refer to DT1's cols
DT2[DT1, (xcols) := Map(function(x, y) x + (y > 0L), mget(xcols), mget(icols)), on="rn"]
甚至更好的是:
DT2[DT1, (xcols) := Map(function(x, y) x + (y > 0L), .SD, i.SD), .SDcols=xcols, i.SDcols = icols]
答案 1 :(得分:2)
我会考虑像......
inc_em <- with(melt(DT1)[value != 0], split(rn, variable))
for (k in names(inc_em))
DT2[.(rn = inc_em[[k]]), (k) := get(k) + 1, on="rn" ]
# rn a b c
# 1: a 0 3 1
# 2: b 1 3 0
# 3: c 1 1 3