Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rbind only the common columns of two data sets

Tags:

r

I have 2 data frames with different number of columns each. Some of the columns are common between the 2 data frames. How can i rbind only the common columns of the two data frames to a new data frame?

i tried with library(plyr);rbind.fill(A,B) however it sets NA values in the columns that do not match, and this does not help me.

Thanks a lot EC

like image 248
ECII Avatar asked Dec 22 '11 14:12

ECII


People also ask

How do I Rbind two data frames in R with different columns?

Method 1 : Using plyr package rbind. fill() method in R is an enhancement of the rbind() method in base R, is used to combine data frames with different columns. The column names are number may be different in the input data frames. Missing columns of the corresponding data frames are filled with NA.

How do you Rbind two data frames?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

Does Rbind require same number of columns?

So, the column names in both the data frames must be the same if you want to use rbind().

Can you use Rbind for multiple data frames?

The rbind() function in R and the bind_rows() function are the most useful functions when it comes to data manipulation. You can easily bind two data frames of the same column count using rbind() function.


2 Answers

Here is my solution hope i got your question right

df1 <- data.frame(a=rnorm(100), b=rnorm(100), not=rnorm(100))
df2 <- data.frame(a=rnorm(100), b=rnorm(100))

bind1 <- bind1 <- df1[, names(df1) %in% names(df2)]
bind2 <- bind1 <- df1[, names(df2) %in% names(df1)]

rbind(bind1, bind2)
like image 21
Seb Avatar answered Sep 24 '22 22:09

Seb


Use intersect to retrieve the common columns.

dfr1 <- data.frame(x = 1:5, y = runif(5), z = rnorm(5))
dfr2 <- data.frame(w = letters[1:5], x = 6:10, y = runif(5))
common_cols <- intersect(colnames(dfr1), colnames(dfr2))
rbind(
  subset(dfr1, select = common_cols), 
  subset(dfr2, select = common_cols)
)

As pointed out in the comments, you can replace the last line with

rbind(
  dfr1[, common_cols], 
  dfr2[, common_cols]
)

for a small performance and typing improvement.

rbind(
  dfr1[common_cols], 
  dfr2[common_cols]
)

also works but I think that it's a tiny bit less clear.


You can also use dplyr equivalents for the last step.

library(dplyr)
bind_rows(
  dfr1 %>% select({common_cols}), 
  dfr2 %>% select({common_cols})
)
like image 74
Richie Cotton Avatar answered Sep 25 '22 22:09

Richie Cotton