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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With