Anika Masters
2013-Feb-26 18:55 UTC
[R] merging or joining 2 dataframes: merge, rbind.fill, etc.?
#I want to "merge" or "join" 2 dataframes (df1 & df2) into a 3rd (mydf). I want the 3rd dataframe to contain 1 row for each row in df1 & df2, and all the columns in both df1 & df2. The solution should "work" even if the 2 dataframes are identical, and even if the 2 dataframes do not have the same column names. The rbind.fill function seems to work. For learning purposes, are there other "good" ways to solve this problem, using merge or other functions other than rbind.fill? #e.g. These 3 examples all seem to "work" correctly and as I hoped: df1 <- data.frame(matrix(data=c(7, 99, 12) , nrow=1 , dimnames list( NULL , c('a' , 'b' , 'd') ) ) ) df2 <- data.frame(matrix(data=c(88, 34, 12, 44, 56) , nrow=1 , dimnames = list( NULL , c('d' , 'b' , 'x' , 'y', 'c') ) ) ) mydf <- merge(df2, df1, all.y=T, all.x=T) mydf #e.g. this works: library(reshape) mydf <- rbind.fill(df1, df2) mydf #This works: library(reshape) mydf <- rbind.fill(df1, df2) mydf #But this does not (the 2 dataframes are identical) df1 <- data.frame(matrix(data=c(7, 99, 12) , nrow=1 , dimnames list( NULL , c('a' , 'b' , 'd') ) ) ) df2 <- df1 mydf <- merge(df2, df1, all.y=T, all.x=T) mydf #Any way to get "mere" to work for this final example? Any other good solutions?
Hi, You could also try: library(gtools) smartbind(df2,df1) #? a? b? d #1 7 99 12 #2 7 99 12 When df1!=df2 smartbind(df1,df2) #?? a? b? d? x? y? c #1? 7 99 12 NA NA NA #2 NA 34 88 12 44 56 A.K. ----- Original Message ----- From: Anika Masters <anika.masters at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, February 26, 2013 1:55 PM Subject: [R] merging or joining 2 dataframes: merge, rbind.fill, etc.? #I want to "merge" or "join" 2 dataframes (df1 & df2) into a 3rd (mydf).? I want the 3rd dataframe to contain 1 row for each row in df1 & df2, and all the columns in both df1 & df2. The solution should "work" even if the 2 dataframes are identical, and even if the 2 dataframes do not have the same column names.? The rbind.fill function seems to work.? For learning purposes, are there other "good" ways to solve this problem, using merge or other functions other than rbind.fill? #e.g. These 3 examples all seem to "work" correctly and as I hoped: df1 <- data.frame(matrix(data=c(7, 99, 12) ,? nrow=1 ,? dimnames list( NULL ,? c('a' , 'b' , 'd') ) ) ) df2 <- data.frame(matrix(data=c(88, 34, 12, 44, 56) ,? nrow=1 , dimnames = list( NULL ,? c('d' , 'b' , 'x' ,? 'y', 'c') ) ) ) mydf <- merge(df2, df1, all.y=T, all.x=T) mydf #e.g. this works: library(reshape) mydf <- rbind.fill(df1, df2) mydf #This works: library(reshape) mydf <- rbind.fill(df1, df2) mydf #But this does not (the 2 dataframes are identical) df1 <- data.frame(matrix(data=c(7, 99, 12) ,? nrow=1 ,? dimnames list( NULL ,? c('a' , 'b' , 'd') ) ) ) df2 <- df1 mydf <- merge(df2, df1, all.y=T, all.x=T) mydf #Any way to get "mere" to work for this final example? Any other good solutions? ______________________________________________ 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.
Nordlund, Dan (DSHS/RDA)
2013-Feb-26 19:37 UTC
[R] merging or joining 2 dataframes: merge, rbind.fill, etc.?
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Anika Masters > Sent: Tuesday, February 26, 2013 10:56 AM > To: r-help at r-project.org > Subject: [R] merging or joining 2 dataframes: merge, rbind.fill, etc.? > > #I want to "merge" or "join" 2 dataframes (df1 & df2) into a 3rd > (mydf). I want the 3rd dataframe to contain 1 row for each row in df1 > & df2, and all the columns in both df1 & df2. The solution should > "work" even if the 2 dataframes are identical, and even if the 2 > dataframes do not have the same column names. The rbind.fill function > seems to work. For learning purposes, are there other "good" ways to > solve this problem, using merge or other functions other than > rbind.fill? > > #e.g. These 3 examples all seem to "work" correctly and as I hoped: > > df1 <- data.frame(matrix(data=c(7, 99, 12) , nrow=1 , dimnames > list( NULL , c('a' , 'b' , 'd') ) ) ) > df2 <- data.frame(matrix(data=c(88, 34, 12, 44, 56) , nrow=1 , > dimnames = list( NULL , c('d' , 'b' , 'x' , 'y', 'c') ) ) ) > mydf <- merge(df2, df1, all.y=T, all.x=T) > mydf > > #e.g. this works: > library(reshape) > mydf <- rbind.fill(df1, df2) > mydf > > #This works: > library(reshape) > mydf <- rbind.fill(df1, df2) > mydf > > #But this does not (the 2 dataframes are identical) > df1 <- data.frame(matrix(data=c(7, 99, 12) , nrow=1 , dimnames > list( NULL , c('a' , 'b' , 'd') ) ) ) > df2 <- df1 > mydf <- merge(df2, df1, all.y=T, all.x=T) > mydf > > #Any way to get "mere" to work for this final example? Any other good > solutions?If rbind.fill(df1,df2) works, why do you need to use merge? Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204