Re: [R] rbind with partially overlapping column names
Hi all, Thanks for your responses. The merge output works for the test case as Bill pointed out, but is contingent on non-overlapping values. Thanks for pointing this out Ian. My actual dataset needs to allow for overlapping values (sorry for the poor test case) so I will cook up something like Ian and Dennis suggested. Thanks again. Jonathan On Mon, May 16, 2011 at 12:28 AM, Ian Gow iand...@gmail.com wrote: 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 wdun...@tibco.com wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Flowers Sent: Sunday, May 15, 2011 5:41 PM To: r-help@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) bac 1 BA NA 2 BA NA 3 b NAc 4 b NAc Rearrange the columns if that is necessary merge(df1,df2,all=TRUE)[c(a,b,c)] a bc 1A B NA 2A B NA 3 NA bc 4 NA bc Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks in advance. Jonathan [[alternative HTML version deleted]] __ R-help@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@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. [[alternative HTML version deleted]] __ R-help@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] 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. Thanks in advance. Jonathan [[alternative HTML version deleted]] __ R-help@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.
Re: [R] rbind with partially overlapping column names
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 jonathanmflow...@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@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@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.
Re: [R] rbind with partially overlapping column names
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 iand...@gmail.com wrote: 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 jonathanmflow...@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@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@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@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.
Re: [R] rbind with partially overlapping column names
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Flowers Sent: Sunday, May 15, 2011 5:41 PM To: r-help@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) bac 1 BA NA 2 BA NA 3 b NAc 4 b NAc Rearrange the columns if that is necessary merge(df1,df2,all=TRUE)[c(a,b,c)] a bc 1A B NA 2A B NA 3 NA bc 4 NA bc Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks in advance. Jonathan [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] rbind with partially overlapping column names
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 wdun...@tibco.com wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Flowers Sent: Sunday, May 15, 2011 5:41 PM To: r-help@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) bac 1 BA NA 2 BA NA 3 b NAc 4 b NAc Rearrange the columns if that is necessary merge(df1,df2,all=TRUE)[c(a,b,c)] a bc 1A B NA 2A B NA 3 NA bc 4 NA bc Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks in advance. Jonathan [[alternative HTML version deleted]] __ R-help@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@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@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.