如何聚合某些列,同时将其他列保留在R中? [英] How to aggregate some columns while keeping other columns in R?

查看:96
本文介绍了如何聚合某些列,同时将其他列保留在R中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

I have a data frame like this:

     id  no  age
1    1   7   23
2    1   2   23
3    2   1   25
4    2   4   25
5    3   6   23
6    3   1   23

,我希望将日期范围由 id 汇总成这样的形式:(只需将,如果他们共享相同的 id ,但将年龄保留在那里)

and I hope to aggregate the date frame by id to a form like this: (just sum the no if they share the same id, but keep age there)

    id  no  age
1    1   9   23
2    2   5   25
3    3   7   23

如何使用R做到这一点?

How to achieve this using R?

推荐答案

假定您的数据框名为 df

aggregate(no~id+age, df, sum)
#   id age no
# 1  1  23  9
# 2  3  23  7
# 3  2  25  5

这篇关于如何聚合某些列,同时将其他列保留在R中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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