The post calculation of anomalies on time-series was very helpful but I have grouped data in my situation. I have a data frame with year, group, and value and columns. Each group has a values for each year. What I want to calculate is the yearly anomaly with in each group. i.e. this year's value minus the mean value over all years for that group. It would be nice to append this anomaly value as a column in the data frame too. Thanks! Here is sample data
year <- c(2000, 2000, 2000, 2000, 2000,2001, 2001, 2001, 2001, 2001,2002, 2002, 2002, 2002, 2002,2003, 2003, 2003, 2003, 2003)
group <- c("A", "B", "C", "D", "A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
value <- runif(20, 0, 1)
df <- as.data.frame(year)
df$group <- group
df$value <- value