I would like to merge two data frames with different columns while columns of the same variable should be added.
This is an example of what I have (the real data frames have about 200 columns each):
A: document v1 v2 v3 1 text1 1 0 0 2 text2 0 0 1 3 text3 0 0 0 B: document v2 v3 v4 1 text1 2 0 1 2 text2 0 1 0 3 text3 1 1 0
What I like to get is:
C: document v1 v2 v3 v4 1 text1 1 2 0 1 2 text2 0 0 2 0 3 text3 0 1 1 0
I tried some variants of merge, bind and join but I can't quite figure it out. Any help would be very welcome!
score:3
Accepted answer
We get the datasets in a list
, rbind
it together, grouped by 'document' get the sum
of each column
library(tidyverse)
list(A, B) %>%
bind_rows %>%
group_by(document) %>%
summarise_all(sum, na.rm = TRUE)
# A tibble: 3 x 5
# document v1 v2 v3 v4
# <chr> <int> <int> <int> <int>
#1 text1 1 2 0 1
#2 text2 0 0 2 0
#3 text3 0 1 1 0
Credit To: stackoverflow.com