Re: [R] rbind with partially overlapping column names

From: Ian Gow <iandgow_at_gmail.com>
Date: Sun, 15 May 2011 23:28:09 -0500


That approach relies on df1 and df2 not having overlapping values in b. Slight variation in df2 gives different results:

> df1 <- data.frame(a=c("A","A"),b=c("B","B"))
> df2 <- data.frame(b=c("B","B"),c=c("c","c"))
> merge(df1,df2,all=TRUE)
  b a c
1 B A c
2 B A c
3 B A c
4 B A c

On 5/15/11 11:19 PM, "William Dunlap" <wdunlap_at_tibco.com> wrote:

>
>> -----Original Message-----
>> From: r-help-bounces_at_r-project.org
>> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Jonathan Flowers
>> Sent: Sunday, May 15, 2011 5:41 PM
>> To: r-help_at_r-project.org
>> Subject: [R] rbind with partially overlapping column names
>>
>> 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.
>
>What is wrong with merge(all=TRUE,...)?
> > merge(df1,df2,all=TRUE)
> b a c
> 1 B A <NA>
> 2 B A <NA>
> 3 b <NA> c
> 4 b <NA> c
>Rearrange the columns if that is necessary
> > merge(df1,df2,all=TRUE)[c("a","b","c")]
> a b c
> 1 A B <NA>
> 2 A B <NA>
> 3 <NA> b c
> 4 <NA> b c
>
>Bill Dunlap
>Spotfire, TIBCO Software
>wdunlap tibco.com
>>
>> 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.
>>
>
>______________________________________________
>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.



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. Received on Mon 16 May 2011 - 04:32:25 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 16 May 2011 - 16:20:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive