The data frame has three columns:
year = c(2000,2000,2001,2001,2001,2002,2002,2002)
sales = c(100,104,106,108,112,115,121, 143)
category = c("Auto", "Personal", "Joint", "Auto", "Personal", "Joint", "Auto", "Personal")
df = data.frame(year, sales, category)
I used the below code to find year over year growth by category but the output adds a new column that doesn't have correct values of the growth or decline percentage. Maybe this is because my code is not accounting for different categories. Any idea on how to do that in R? In SQL you could group by but I tried adding that in the above code in R and that gave me a new column with 0 in all rows?!
setDT(df)[, new.col := Sales/shift(Sales) - 1]