combine the row with the different variable

i want to combining 2 dataframe but there are diferent variable
example

df1=data.frame(x1=c(1,1,1,2,2,2,2),x2=c("a","a","b","b","c","c","c"),x3=c("t","u","v","w","x","y","z"),x4=c("apple","apple","mango","mango","mango","mango","mango"))

df2=data.frame(x1=c(1,1,1,2,2,2,2),x2=c("a","a","b","b","c","c","c"),x4=c("apple","banana","banana","melon","melon","melon","melon"),x5=c("t","u","v","w","x","y","z"))

i’d tried with cbind, rbind, merge, right and left join, but no one can like I expected. my expectation in df is there 13 row and 5 column. why 13 column? because in row 1 can combined

>Solution :

You could do:

full_join(mutate(df1, x6 = x3), mutate(df2, x3 = x5)) %>% select(x1, x2, x3 = x6, x4, x5)

   x1 x2   x3     x4   x5
1   1  a    t  apple    t
2   1  a    u  apple <NA>
3   1  b    v  mango <NA>
4   2  b    w  mango <NA>
5   2  c    x  mango <NA>
6   2  c    y  mango <NA>
7   2  c    z  mango <NA>
8   1  a <NA> banana    u
9   1  b <NA> banana    v
10  2  b <NA>  melon    w
11  2  c <NA>  melon    x
12  2  c <NA>  melon    y
13  2  c <NA>  melon    z

Leave a Reply