Merging tow dataframes in R that do not have common columns

Advertisements

I have been trying to merge two dataframes in r that do not have the same column. The output I get is one that has an extra set of rows and duplicated values. My attempt is below and edits to the code a welcome.

df1 <- data.frame(A=c("A","B", "C", "D", "E"),
                  B=c(7, 9, 8, 3, 2),
                  C=c(3, 5, 2, 9, 9))

df2 <- data.frame(D=c(1, 3, 3, 4, 5),
                  E=c(7, 7, 8, 3, 2))

df3<-merge(x=df1, y=df2, by=NULL)

This works but now the output gives me 25 observations of 5 variables. Where the is an extra set of rows like a duplication. ( disclaimer this is not the entire dataset here just a subset of it) .


     A     B     C     D     E  
1 A      7      3     1     7 
2 B      9      5     1     7 
3 C      8      2     1     7
4 D      3      9     1     7 
5 E      2      9     1     7 
6 A      7      3     3     7 
7 B      9      5     3     7 
8 C      8      2     3     7 

My desired output is something like the one below (5 observations and 5 variables), where I do not an additional set of rows and duplicated values.

 A     B     C     D     E  
1 A      7      3     1     7 
2 B      9      5     3     7 
3 C      8      2     3     8
4 D      3      9     4     3 
5 E      2      9     5     2 

>Solution :

As some of the comments already mentioned you can use cbind() to accomplish that.

df3 <- cbind(df1, df2)

Leave a ReplyCancel reply