Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union of dataframes in R by rownames

Tags:

dataframe

r

union

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?

like image 966
CodeKingPlusPlus Avatar asked May 06 '13 22:05

CodeKingPlusPlus


1 Answers

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)
like image 139
flodel Avatar answered Oct 24 '22 21:10

flodel