r - Calculating change of values between same day in different years -
i need calculate called mat (movie anual total), means % change in sales value between same day in 2 different year:
id sales day month year 500 31 12 2015 100 1 1 2016 200 2 1 2016 ... 200 1 1 2017
does have idea how deal it?
i want this:
id sales day month yeas **mat**
with way data set up, you're quite close. want group data month , day, order each group year, , take successive differences (assuming want mat sequential years)
library(lubridate) library(dplyr) x <- data.frame(date = seq(as.date("2014-01-01"), as.date("2017-12-31"), = 1)) %>% mutate(day = day(date), month = month(date), year = year(date), sales = rnorm(nrow(.), mean = 100, sd = 5)) x %>% group_by(month, day) %>% arrange(month, day, year) %>% mutate(mat = c(na, diff(sales))) %>% ungroup()
if wanting able generically take difference between 2 years, need refinements.
Comments
Post a Comment