'How to join a data frame on top of another without changing values using Tidyverse
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 columns 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 1:[1]
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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rdelrossi |
| Solution 2 | Zhiqiang Wang |
