r 聚合数据框:一些列未更改,一些列聚合 [英] r aggregate dataframe: some columns unchanged, some columns aggregated
本文介绍了r 聚合数据框:一些列未更改,一些列聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾尝试查看有关聚合、应用等的手册,但我找不到示例,其中某些列被正在应用的函数跳过,而其他列则按原样复制.示例:
I have tried to look at manuals for aggregate, apply, etc, but I can't find examples where some columns are skipped by the function being applied, while other columns are copied as-is. Example:
> olddf = data.frame(code=c("one","one","two"), val1=c(1,2,3), val2=c(4,5,6), val3=c(7,8,9))
> olddf
code val1 val2 val3
1 one 1 4 7
2 one 2 5 8
3 two 3 6 9
>
如何聚合 olddf 以便获得新的数据框,其中:
How do I aggregate olddf so that I get a new data frame where:
- 代码列保持原样,
- val1 被跳过
- val2 通常聚合,例如sum()
- 和一个新列是基于较早的聚合之一创建的......例如新列 = sum(val3)/sum(val2)?
- code column is kept as-is,
- val1 is skipped
- val2 is aggregated normally e.g. sum()
- and a new column is created based on one of the earlier aggregates... e.g. new column = sum(val3)/sum(val2)?
我基本上想要:
> newdf
code val2 newcol
1 one 9 1.6666
2 two 6 1.5000
我想一步完成,而不是定义一个单独的函数来处理每一列/聚合.这可能吗?
I would like to do this in one step rather than defining a separate function to deal with each column / aggregate. Is this possible?
推荐答案
Try data.table
library(data.table)
setDT(olddf)[, .(val2 = sum(val2),
newcol = sum(val3)/sum(val2)), by = code]
# code val2 newcol
# 1: one 9 1.666667
# 2: two 6 1.500000
这篇关于r 聚合数据框:一些列未更改,一些列聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文