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
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)
made my own function for my personal package: (this works also for more than 2 dataframes)
function:
fast.rbind <- function(...,method=c("fill","common"),value=NA){
if("fill"==method[1]) {
fun1 <- function(x,y,value=NA){
x[setdiff(colnames(y),colnames(x))] <- value
y[setdiff(colnames(x),colnames(y))] <- value
return(rbind(x,y))
}
}
if("common"==method[1]) {
fun1 <- function(x,y,value=NULL){
common_cols <- intersect(colnames(x), colnames(y))
return(rbind(x[, common_cols,drop=F],y[, common_cols,drop=F]))
}
}
return(Reduce(function(x,y){fun1(x=x,y=y,value=value)},list(...)))
}
call + example data:
df1 <- mtcars[1:5,1:4]
df2 <- mtcars[6:10,2:5]
df3 <- mtcars[11:15,4:7]
fast.rbind(df1,df2,df3,method="common")
fast.rbind(df1,df2,df3,value="yourDesiredFill")
Use intersect
to retrieve the common columns.
dfr1 <- data.frame(x = 1:5, y = runif(5), z = rnorm(5))
dfr2 <- data.frame(w = letter[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.