Union of data frames in R by growth names

I have 4 data frames, each of which is an index in a list. I would like to combine them as a single frame. In a given language from mathematics, it would be more reasonable if it were an association on the names of rosers. 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 only works with vectors. How can I make this work on dataframes?

  • How can I translate this to R?
  • Is there a better way to achieve the desired result?

EDIT: How to keep growth names after merging?

+4
source share
2 answers

Connect them together first:

 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 this is not the case, then you may be interested in the rbind.fill function in the plyr package:

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

Then, to remove duplicates, if you need (according to the established union):

 df.union <- unique(df.cat) 
+12
source

You can combine dataframes with the merge function. Since you have several data frames, you can use Reduce to combine them all at once.

 merged.data <- Reduce(function(...) merge(...), list(dfSub[[1]], dfSub[[2]], dfSub[[3]], dfSub[[4]]) 

As an example:

 > people <- c('Bob', 'Jane', 'Pat') > height <- c(72, 64, 68) > weight <- c(220, 130, 150) > age <- c(45, 32, 35) > height.data <- data.frame(people, height) > weight.data <- data.frame(people, weight) > age.data <- data.frame(people, age) > height.data people height 1 Bob 72 2 Jane 64 3 Pat 68 > weight.data people weight 1 Bob 220 2 Jane 130 3 Pat 150 > age.data people age 1 Bob 45 2 Jane 32 3 Pat 35 > Reduce(function(...) merge(...), list(height.data, weight.data, age.data)) people height weight age 1 Bob 72 220 45 2 Jane 64 130 32 3 Pat 68 150 35 
-2
source

Source: https://habr.com/ru/post/1479443/


All Articles