Skip to content
Back to formatted view

Raw Message

Message-ID: <C9F5DEF9.AEAC%iandgow@gmail.com>
Date: 2011-05-16T00:50:31Z
From: Ian Gow
Subject: rbind with partially overlapping column names
In-Reply-To: <BANLkTin_Nx4P6CebyLK5w6B51vVzAiaG0Q@mail.gmail.com>

Hi:

This is a bit of a kluge, but works for your test case:

> df2[,setdiff(names(df1),names(df2))] <- NA
> df1[,setdiff(names(df2),names(df1))] <- NA
> df3 <- rbind(df1,df2)
> df3
a b c
1 A B <NA>
2 A B <NA>
3 <NA> b c
4 <NA> b c

-Ian


On 5/15/11 7:41 PM, "Jonathan Flowers" <jonathanmflowers at gmail.com> wrote:

>Hello,
>
>I would like to merge two data frames with partially overlapping column
>names with an rbind-like operation.
>
>For the follow data frames,
>
>df1 <- data.frame(a=c("A","A"),b=c("B","B"))
>df2 <- data.frame(b=c("b","b"),c=c("c","c"))
>
>I would like the output frame to be (with NAs where the frames don't
>overlap)
>
>a      b     c
>A     B     NA
>A     B     NA
>NA   b     c
>NA   b     c
>
>I am familiar with ?merge and ?rbind, but neither seem to offer a means to
>accomplish this.
>
>Thanks in advance.
>
>Jonathan
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>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.