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

How to join a data frame on top of another without changing values using Tidyverse

Hello Everyone I hope you are all having a great day,

I am working a multiple data.frames that I have to join on top of each other, which is kind of what I would expect from the function full_join of tidyverse to illustrate I have the following datasets

name<-c("AAA","AAA","AAA")
value<-c(1:3)
tag<-c(0,0,0)

part_a<-data.frame(name,value,tag)

name<-c("AAA","AAA","AAA")
value<-c(1:3)
key<-c(1,1,1)

part_b<-data.frame(name,value,key)

My desired output would be something like this:

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

name value tag key
AAA 1 0 NA
AAA 2 0 NA
AAA 3 0 NA
AAA 1 NA 1
AAA 2 NA 1
AAA 3 NA 1

but instead I am getting this:

> full_join(part_a,part_b)
Joining, by = c("name", "value")
  name value tag key
1  AAA     1   0   1
2  AAA     2   0   1
3  AAA     3   0   1

Which is very confusing to me as I think this function is trying to find common values and then aggreate the rest of the data but what I really want is just to put all dataframes on top of each other including the colummns that they do not have in common, I know I cannot use rbind since this function requires dataframes to have the same column names, I would be so thankfull if you guys can help me out!

>Solution :

full_join() is merging your data frames. Since you didn’t specify the columns to use as identifiers, it’s using the common fields (i.e., name and value). To simply combine data frames you can use dplyr‘s bind_rows():

result <- bind_rows(part_a, part_b)

Note that there’s also a bind_cols() for combining variables (i.e., columns) from multiple data frames into a single one.

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