首页 > 解决方案 > 合并表列表而不更改标签顺序

问题描述

我正在尝试按第一列合并表列表,但顺序正在改变,我希望行的顺序应该与第一次迭代相同。

so the output of my first function is list of dataframes

df_list1 <- data.frame(c= c("tml 14","tml 26","tml 73","tml 24","tml 95","tml 60","tml 77","tml 8","tml 19"),
                  "CAD"=c(399,590,287,353,483,591,184,501,590))
df_list2 <- data.frame(c= c("tml 14","tml 26","tml 73","tml 24","tml 95","tml 60","tml 77","tml 8","tml 19"),
                  "NHA"=c(556,188,446,583,398,505,461,737,164))
df_list3 <- data.frame(c= c("tml 14","tml 26","tml 73","tml 24","tml 95","tml 60","tml 77","tml 8","tml 19"),
                  "BHS"=c(356,337,712,255,573,598,622,144,159))
df_list4 <- data.frame(c= c("tml 14","tml 26","tml 73","tml 24","tml 95","tml 60","tml 77","tml 8","tml 19"),
                  "KLI"=c(686,630,627,498,551,159,253,448,437))

my function1 is like 


function1 <- df_list2[[i]]<- function(df,var,rownames[i]) 
df_list[i]<- do.call(rbind,df_list2)

now df is the output list like df1 , df2, df3.......

and I want to join those list of data frames to create one table
and I am trying like below

tbl<-Reduce(function(...)merge(...,all=TRUE,by="C"),df_list)

我们有什么解决方案可以让我保持相同的行顺序...??

输出就像

在此处输入图像描述

标签: r

解决方案


我们可以使用full_joinwith reducewhich 将以相同的顺序给出输出

library(dplyr)
library(purrr)
library(stringr)
reduce(mget(str_c('df_list', 1:4)), full_join, by = 'c')

推荐阅读