R中的数据帧与rownames的联合 [英] Union of dataframes in R by rownames
问题描述
U< - union(dfSub [[1]],dfSub [[2] ],dfSub [[3]],dfSub [[4]])
code> union 函数是它只对向量进行操作。如何获得这个数据框架?
- 如何将其翻译成R?
- 是否有更好的方法来实现所需的结果?
编辑:联合?
首先,绑定在一起:
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?
- How can I translate this into R?
- 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屋!