按行名合并多个数据帧 [英] Merge Multiple Data Frames by Row Names
问题描述
我知道如何使用两个数据框:
x = data.frame(a = c(1,2,3),row.names = letters [1:3])
y = data.frame (b = c(1,2,3),row.names = letters [1:3])
merge(x,y,by =row.names)
但是当我尝试使用 reshape
package的 merge_all()
我收到一个错误。
z = data.frame(c = c(1, 2,3),row.names = letters [1:3])
l = list(x,y,z)
merge_all(l,by =row.names)
-ncol(df)中的错误:一元运算符的无效参数
什么是最好的方法这样做?
合并 row.names
做奇怪的事情 - 它创建一个名为Row.names的列,这使得后续的合并变得困难。
为了避免这个问题,您可以创建一个列名称(通常是更好的)理想无论如何 - 行名非常有限,难以操纵)。使用OP中给出的数据的一种方式(不是最佳方式,更好的和更简单的处理矩形数据的方法我建议知道 data.table
代替):
减少(merge,lapply(l,function(x)data.frame(x,rn = row。 name(x))))
I'm trying to merge multiple data frames by row names.
I know how to do it with two:
x = data.frame(a = c(1,2,3), row.names = letters[1:3])
y = data.frame(b = c(1,2,3), row.names = letters[1:3])
merge(x,y, by = "row.names")
But when I try using the reshape
package's merge_all()
I'm getting an error.
z = data.frame(c = c(1,2,3), row.names = letters[1:3])
l = list(x,y,z)
merge_all(l, by = "row.names")
Error in -ncol(df) : invalid argument to unary operator
What's the best way to do this?
Merging by row.names
does weird things - it creates a column called Row.names, which makes subsequent merges hard.
To avoid that issue you can instead create a column with the row names (which is generally a better idea anyway - row names are very limited and hard to manipulate). One way of doing that with the data as given in OP (not the most optimal way, for more optimal and easier ways of dealing with rectangular data I recommend getting to know data.table
instead):
Reduce(merge, lapply(l, function(x) data.frame(x, rn = row.names(x))))
这篇关于按行名合并多个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!