Hi, I have a following problem. I need to calculate maximum value per each name from a previous year.
Let:
dfA <- data.frame(year = c(2003,2003,2004,2004,2002,2004,2004,2005, 2010),
name = c("A","A","A","A","B","B","B","B", "B" ) ,
value = c(5,7,1,2,10,6,8,15, NA))
Desired output is
dfB <- data.frame(year = c(2003,2003,2004,2004,2002,2004,2004,2005, 2010),
name = c("A","A","A","A","B","B","B","B", "B" ) ,
value = c(5,7,1,2,10,6,8,15, NA),
max_lag = c(NA,NA,7,7,NA,NA,NA,8, NA))
I tried this, but it calculated maximum from current years:
dfA$year_lag <- dfA$year -1
dfB <- dfA %>% arrange(name, year_lag) %>%
group_by(name, year_lag) %>%
dplyr::mutate(max_lag= max(value, na.rm = TRUE) )
Thanks for help!