Union of dataframes in R by rownames

dataframe, r, union

Solution

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)

Problem

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?

Original source