R中的数据帧与rownames的联合 [英] Union of dataframes in R by rownames

查看:184
本文介绍了R中的数据帧与rownames的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个数据帧,每个索引在列表中。我想把它们整合为一个数据框。在数学中的设定语言中,最有意义的是,它是在rownames上的联合。所以我可能会这样:

  U<  -  union(dfSub [[1]],dfSub [[2] ],dfSub [[3]],dfSub [[4]])

code> union 函数是它只对向量进行操作。如何获得这个数据框架?


  1. 如何将其翻译成R?

  2. 是否有更好的方法来实现所需的结果?

编辑:联合?

解决方案

首先,绑定在一起:

  df.cat<  -  rbind(dfSub [[1]],dfSub [[2]],dfSub [[3]],dfSub [[4]])

或更好:

  df.cat<  -  do.call(rbind,dfSub [1:4])

步骤要求所有data.frames具有相同的列名称。如果不是这样,那么您可能对 plyr 包中的 rbind.fill 函数感兴趣:

 库(plyr)
df.cat< - rbind.fill(dfSub [1:4])






然后,如果需要,删除重复联合会):

  df.union<  -  unique(df.cat)


I have 4 dataframes, each the index in a list. I would like to combine them altogether as one dataframe. In set language from mathematics, it would make most sense for this to be the union on the rownames. So I might have something like this:

U <- union(dfSub[[1]], dfSub[[2]], dfSub[[3]], dfSub[[4]])

The problem with the union function is that it operates only on vectors. How can I get this to work on dataframes?

  1. How can I translate this into R?
  2. Is there a better way of achieving the desired result?

EDIT: How can I preserve rownames after the union?

解决方案

First, bind them together:

df.cat <- rbind(dfSub[[1]], dfSub[[2]], dfSub[[3]], dfSub[[4]])

or better:

df.cat <- do.call(rbind, dfSub[1:4])

This first step requires that all data.frames have the same column names. If it is not the case, then you might be interested in the rbind.fill function from the plyr package:

library(plyr)
df.cat <- rbind.fill(dfSub[1:4])


Then, to remove duplicates if you need (as a set union would):

df.union <- unique(df.cat)

这篇关于R中的数据帧与rownames的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆