那么如何添加两个数据框,其中为相同的row.names添加行:
x = data.frame(x = 1:10, y = 10:1); y = x
rownames(y) = as.numeric(rownames(x)) + 5
x + y #WRONG, rows should be offset by '5', producing a data.frame that has 15 rows.
可以使用ddply,通过添加索引列,这样做(也显示我想要的结果):
x$id = as.numeric(rownames(x))
y$id = as.numeric(rownames(y))
plyr::ddply(rbind(x,y),'id',function(x){
colSums(x[,c('x','y')])
})[,-1]
目标结果:
x y
1 1 10
2 2 9
3 3 8
4 4 7
5 5 6
6 7 15
7 9 13
8 11 11
9 13 9
10 15 7
11 6 5
12 7 4
13 8 3
14 9 2
15 10 1
在上文中,行6:10是两个数据帧的交集的总和,与索引列相交。
答案 0 :(得分:4)
试试这个 -
a <- rownames(x)
b <- rownames(y)
rbind(x[!(a %in% b),], x[intersect(a, b),] +
y[intersect(a, b),], y[!(b %in% a),])
答案 1 :(得分:2)
# I would use aggregate
d <- aggregate(rbind.data.frame(x, y),
list(rowname = c(rownames(x), rownames(y))),
sum)
# if you want the rows ordered as before
d <- d[order(as.numeric(d$rowname)), ]
# rowname x y
# 1 1 1 10
# 8 2 2 9
# 9 3 3 8
# 10 4 4 7
# 11 5 5 6
# 12 6 7 15
# 13 7 9 13
# 14 8 11 11
# 15 9 13 9
# 2 10 15 7
# 3 11 6 5
# 4 12 7 4
# 5 13 8 3
# 6 14 9 2
# 7 15 10 1
答案 2 :(得分:1)
您可以按行名称进行合并,这通常是不可取的。然后你得到相同的x和y的索引并取一行总和。
xx=merge( x, y, by=0, all=TRUE)
l=lapply(names(x), function(yy) grep(paste('^',yy,'.*', sep = ''), names(xx) ))
df=as.data.frame(sapply(l, function(yy) rowSums(xx[,yy], na.rm = T)))
names(df)=names(x)
df[order(as.numeric(xx$Row.names)),]
x y
1 1 10
8 2 9
9 3 8
10 4 7
11 5 6
12 7 15
13 9 13
14 11 11
15 13 9
2 15 7
3 6 5
4 7 4
5 8 3
6 9 2
7 10 1