Merging more than 2 dataframes in R by rownames

后端 未结 4 2021
醉梦人生
醉梦人生 2020-12-04 19:28

I gather data from 4 df\'s and would like to merge them by rownames. I am looking for an efficient way to do this. This is a simplified version of the data I have.

4条回答
  •  悲&欢浪女
    2020-12-04 20:13

    join_all from plyr will probably do what you want. But they all must be data frames and the rownames are added as a column

    require(plyr)
    
    df3 <- data.frame(df3)
    df4 <- data.frame(df4)
    
    df1$rn <- rownames(df1)
    df2$rn <- rownames(df2)
    df3$rn <- rownames(df3)
    df4$rn <- rownames(df4)
    
    df <- join_all(list(df1,df2,df3,df4), by = 'rn', type = 'full')
    

    type argument should help even if the rownames vary and do not match If you do not want the rownames:

    df$rn <- NULL
    

提交回复
热议问题