[R] merging data.frames columnwise (rbind with different variables, lengths)

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Dec 11 13:52:41 CET 2008


have a look at merge(), e.g.,

df1 <- data.frame(A = c(1,2), B = c("m","f"), C = c("at home", "away"))
df2 <- data.frame(A = c(2), C = c("at home"))

merge(df1, df2, all = TRUE, sort = FALSE)


I hope it helps.

Best,
Dimitris


Stefan Uhmann wrote:
> Dear List,
> 
> I have two dataframes with overlapping colnames and want to merge them. 
>  Actually, what I want is more similar to rbind, but the dataframes 
> differ in their columns. Here are the examples:
> 
> df1 <- data.frame(A = c(1,2), B = c("m","f"), C = c("at home", "away"))
> df2 <- data.frame(A = c(2), C = c("at home"))
> 
> Here the desired result:
> 
>   A B        C
> 1 1 m  at home
> 2 2 f     away
> 3 2 NA at home
> 
> Thanks for any help,
> Stefan
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list