Skip to content

rbind with partially overlapping column names

6 messages · Ian Gow, Dennis Murphy, William Dunlap +1 more

#
Hi:

This is a bit of a kluge, but works for your test case:
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:

            
#
Hi:

Another way, with a little less typing but using the same principle, is

df1$c <- df2$a <- NA
rbind(df1, df2)

Dennis
On Sun, May 15, 2011 at 5:50 PM, Ian Gow <iandgow at gmail.com> wrote:
#
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
#
That approach relies on df1 and df2 not having overlapping values in b.
Slight variation in df2 gives different results:
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: