基于日期键汇总多个R列表的值 [英] Summing the values of several R Lists based on a date key

查看:54
本文介绍了基于日期键汇总多个R列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个有两个列表的脚本,并且试图合并结果以便获得一个新列表.每个列表都有一个日期,然后是两个数字.列表如下所示:

I am working on a script where I have two lists and I am trying to combine the results so I get a new list. Each list has a date and then two numbers. The lists look like this:

         date clicks impressions
1  2019-06-01      1           2 
2  2019-06-02      0           0 
3  2019-06-03      100         120 

        date clicks impressions
1  2019-06-01      2          14
2  2019-06-02      3          14
3  2019-06-03     11          29

我想要一个清单

        date clicks impressions
1  2019-06-01      3          16
2  2019-06-02      3          14
3  2019-06-03     111         149

完成此操作的最佳方法是什么.随着时间的推移,我将再添加20-30个列表,因此,我想拉第一个列表,然后与第二个列表结合,然后再合并第三个列表,依此类推.我不知道是否可以假设每个日期都在每个列表中.

What is the best way to accomplish this. In time I will have 20 - 30 more lists that will be added to this, so I'll want to pull the first list and then combine with the second and then a third and so on. I don't know if I'll be able to assume that each date will be in each list.

推荐答案

假设您的列表名为list_df,则可以使用bind_rowsgroup_by date然后使用sum将所有内容绑定在一起其他列.

Assuming your list is called list_df, you can bind them all together using bind_rows, group_by date and then sum all the other columns.

library(dplyr)

list_df %>%
   bind_rows() %>%
   group_by(date) %>%
   summarise_all(sum)

# A tibble: 3 x 3
#  date       clicks impressions
#  <fct>       <int>       <int>
#1 2019-06-01      3          16
#2 2019-06-02      3          14
#3 2019-06-03    111         149

可以使用Reduce

aggregate(.~date, Reduce(rbind, list_df), sum)

这篇关于基于日期键汇总多个R列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆