Home > Software engineering >  How to join a data frame on top of another without changing values using Tidyverse
How to join a data frame on top of another without changing values using Tidyverse

Time:03-04

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:

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!

CodePudding user response:

library(dplyr)
part_a |> bind_rows(part_b)

  name value tag key
1  AAA     1   0  NA
2  AAA     2   0  NA
3  AAA     3   0  NA
4  AAA     1  NA   1
5  AAA     2  NA   1
6  AAA     3  NA   1

CodePudding user response:

You can use dplyr's bind_rows():

result <- bind_rows(part_a, part_b)

  • Related