Merge Multiple Data Frames by Row Names

前端 未结 3 548
谎友^
谎友^ 2021-02-03 13:34

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.fra         


        
3条回答
  •  独厮守ぢ
    2021-02-03 13:47

    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))))
    

提交回复
热议问题