按行分组值差异 [英] Group By Row Value Difference
本文介绍了按行分组值差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,其结构类似于:年,月,ValueA,ValueB,ValueC等.我想按年和月对表进行分组,但要根据列值的差异进行汇总.
I have a table that has structure like: Year, Month, ValueA, ValueB, ValueC, etc.. I want to group the table by Year and Month, but aggregate based on the difference in column values.
Year Month ValueA ValueB ValueC
2016 1 40 53 49
2017 2 29 31 26
2016 1 25 20 31
2017 2 22 30 29
我要输出一个看起来像这样的表:
I want to output a table that looks like:
Year Month ValueA ValueB ValueC
2016 1 15 33 18
2017 2 7 1 3
我将如何处理?非常感谢您的帮助.
How would I go about this? Any help is much appreciated.
推荐答案
我们可以使用基数R aggregate
并按 Year
和 Month
分组计算两行之间的差异.
We can use base R aggregate
and group by Year
and Month
to calculate the difference between the two rows.
abs(aggregate(.~Year + Month, df, diff))
# Year Month ValueA ValueB ValueC
#1 2016 1 15 33 18
#2 2017 2 7 1 3
这篇关于按行分组值差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文