问题
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