1

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Dom
  • 187
  • 3
  • 14

3 Answers3

4

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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
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)))
tbradley
  • 2,210
  • 11
  • 20
1

You can use approach described in this thread using plyr:

ddply(df, .(Year, Month), numcolwise(diff))
Community
  • 1
  • 1