Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading