Suppose I have the following data frame
Website <- rep(paste(\"Website\",1:3),2)
Year <- c(rep(2013,3),rep(2014,3))
V1 <- c(10,20,50,20,30,70)
V2 &l
Assuming that you have more years, dplyr handles it beautifully.
library(dplyr)
growth <- function(x)x/lag(x)-1
df %>%
group_by(Website) %>%
mutate_each(funs(growth), V1, V2)
# Website Year V1 V2
#1 Website 1 2013 NA NA
#2 Website 2 2013 NA NA
#3 Website 3 2013 NA NA
#4 Website 1 2014 1.0 2.0
#5 Website 2 2014 0.5 1.0
#6 Website 3 2014 0.4 0.5