Combine two data frames with different columns
1
0
Entering edit mode
Daniel Brewer ★ 1.9k
@daniel-brewer-1791
Last seen 9.7 years ago
I would like to join two dataframes that have the majority of columns in common but some different. Let me give you an example: Dataframe 1 ID COL1 COL2 COL3 1 a a a 2 b b b 3 c c c Dataframe 2 ID COL1 COL2 COL4 4 a a d 5 b b d And the joined dataframe I would like is: ID COL1 COL2 COL3 COL4 1 a a a NA 2 b b b NA 3 c c c NA 4 a a NA d 5 b b NA d Any ideas? Dan ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
Cancer Cancer • 991 views
ADD COMMENT
0
Entering edit mode
@sean-davis-490
Last seen 4 months ago
United States
On Feb 1, 2008 7:28 AM, Daniel Brewer <daniel.brewer at="" icr.ac.uk=""> wrote: > I would like to join two dataframes that have the majority of columns in > common but some different. Let me give you an example: > > Dataframe 1 > > ID COL1 COL2 COL3 > 1 a a a > 2 b b b > 3 c c c > > Dataframe 2 > > ID COL1 COL2 COL4 > 4 a a d > 5 b b d > > And the joined dataframe I would like is: > > ID COL1 COL2 COL3 COL4 > 1 a a a NA > 2 b b b NA > 3 c c c NA > 4 a a NA d > 5 b b NA d > > Any ideas? One solution is in the CRAN package reshape: df1 <- data.frame(matrix(letters[1:16],nc=4)) df2 <- data.frame(matrix(letters[1:16],nc=4)) colnames(df1) <- 1:4 colnames(df2) <- 2:5 rbind.fill(df1,df2) 1 2 3 4 5 1 a e i m <na> 2 b f j n <na> 3 c g k o <na> 4 d h l p <na> 5 <na> a e i m 6 <na> b f j n 7 <na> c g k o 8 <na> d h l p There are certainly other answers, but I find the reshape package to be a useful one anyway. Sean
ADD COMMENT
0
Entering edit mode
> On Feb 1, 2008 7:28 AM, Daniel Brewer <daniel.brewer at="" icr.ac.uk=""> wrote: >> I would like to join two dataframes that have the majority of columns in >> common but some different. Let me give you an example: >> >> Dataframe 1 >> >> ID COL1 COL2 COL3 >> 1 a a a >> 2 b b b >> 3 c c c >> >> Dataframe 2 >> >> ID COL1 COL2 COL4 >> 4 a a d >> 5 b b d >> >> And the joined dataframe I would like is: >> >> ID COL1 COL2 COL3 COL4 >> 1 a a a NA >> 2 b b b NA >> 3 c c c NA >> 4 a a NA d >> 5 b b NA d >> >> Any ideas? > > One solution is in the CRAN package reshape: > > df1 <- data.frame(matrix(letters[1:16],nc=4)) > df2 <- data.frame(matrix(letters[1:16],nc=4)) > colnames(df1) <- 1:4 > colnames(df2) <- 2:5 > rbind.fill(df1,df2) > > 1 2 3 4 5 > 1 a e i m <na> > 2 b f j n <na> > 3 c g k o <na> > 4 d h l p <na> > 5 <na> a e i m > 6 <na> b f j n > 7 <na> c g k o > 8 <na> d h l p > > There are certainly other answers, but I find the reshape package to > be a useful one anyway. An alternative is: merge(df1, df2, all=TRUE)
ADD REPLY

Login before adding your answer.

Traffic: 397 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6