合并两个数据框并汇总 [英] combine two data frames and aggregate
本文介绍了合并两个数据框并汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的2个数据帧:
I am having 2 data frames in the below format:
dt1
id col1 col2 col3 col4
___ ____ ____ _____ _____
1 2 3 1 2
2 3 4 1 1
3 1 1 1 1
4 1 2 1 2
5 1 1 1 1
6 1 2 1 2
dt2
id col1 col2 col3 col4
___ ____ ____ _____ _____
1 1 3 1 2
2 3 4 1 0
4 1 1 1 1
6 1 2 1 2
9 2 1 1 1
12 1 2 1 2
我想通过id和生成的数据帧将这两个数据帧聚合并合并在一起
and I want to aggregate and combine these two data frames by the id and the resulting dataframe like
dt3
id col1 col2 col3 col4
___ ____ ____ _____ _____
1 3 6 2 4
2 6 8 2 1
3 1 1 1 1
4 2 3 2 3
5 1 1 1 1
6 2 4 2 4
9 2 1 1 1
12 1 2 1 2
我尝试了 dt3 = merge(dt1,dt2,all = TRUE)
但没有用,也尝试了 dt3 = merge(dt1,dt2,by = id)
也不起作用.感谢您的帮助.
I tried with dt3=merge(dt1,dt2,all=TRUE)
but did not work.Also tried dt3=merge(dt1,dt2,by=id)
too did not work.Any help is appreciated.
推荐答案
我们可以在 data.table
中使用 rbindlist
并获取 sum
按"id"分组后各列的大小
We can use rbindlist
in data.table
and get the sum
of each column after grouping by 'id'
library(data.table)
rbindlist(mget(paste0('dt', 1:2)))[, lapply(.SD, sum), by = id]
# id col1 col2 col3 col4
#1: 1 3 6 2 4
#2: 2 6 8 2 1
#3: 3 1 1 1 1
#4: 4 2 3 2 3
#5: 5 1 1 1 1
#6: 6 2 4 2 4
#7: 9 2 1 1 1
#8: 12 1 2 1 2
或者将 bind_rows
与 tidyverse
librarydplyr)
bind_rows(dt1, dt2) %>%
group_by(id) %>%
summarise_each(funs(sum))
这篇关于合并两个数据框并汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文