Group By Row Value Difference [duplicate]

不想你离开。 提交于 2020-12-06 12:57:30

问题


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.


回答1:


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



回答2:


Here is a way using the dplyr package:

library(tidyverse)
df <- data.frame(Year = c(2016, 2017, 2016, 2017),
             Month = c(1, 2, 1, 2),
             ValueA = c(40, 29, 25, 22),
             ValueB = c(53, 31, 20, 30),
             ValueC = c(49, 26, 31, 29))

df1 <- df %>%
  group_by(Year, Month) %>%
  summarize(ValueA = abs(diff(ValueA)), ValueB = abs(diff(ValueB)), ValueC = abs(diff(ValueC)))



回答3:


You can use approach described in this thread using plyr:

ddply(df, .(Year, Month), numcolwise(diff))


来源:https://stackoverflow.com/questions/42442850/group-by-row-value-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!