gvkey <- c(1, 1, 1, 1, 2,2,2, 4, 4 )

Fyear <- c(2005,2006,2007,2008, 2007,2008,2009 , 2011,2012)

cashflow <- c(100, 110, 120, 130, 500, 550, 600, 50, 60)

lagAT <- c(1000,1500,1300,1200, 300,500, 800, 70, 40)set1 <- data.frame(gvkey, Fyear, cashflow, lagAT)

set1 <- set1%>%

group_by(gvkey) %>%

arrange(Fyear) %>%

mutate(difcash = (cashflow - lag(cashflow)))set1 <- set1%>%

group_by(gvkey) %>%

arrange(Fyear) %>%

mutate(yoy2= difcash/lagAT)

Dear everyone,

In my datasheet I am trying to calculate the year on year growth in cashflow for a number of companies throughout the years. I have been trying to calculate this difference via the code above.

But this method does not introduce a condition that he shouldn't use the 'cashflow' data if the 'gvkey' is different.

Does anyone know how I can add this condition into my code so that i can calculate the true growth?

Thank you in advance!