我想合并两个具有不同列的数据框,而应添加相同变量的列。
这是我所拥有的示例(实际数据帧每个都有大约200列):
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
我喜欢得到的是:
C: document v1 v2 v3 v4
1 text1 1 2 0 1
2 text2 0 0 2 0
3 text3 0 1 1 0
我尝试了合并,绑定和联接的一些变体,但我不太清楚。任何帮助将非常欢迎!
我们得到数据集的list
,rbind
一起,通过“文件”组合获得sum
每列
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句