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?
This returns just what I would expect. All I added is an arrange() at the end to make it easier to evaluate the calculations. Do you get something different?
Thank you for your reply.
In my desired output the year on year growth is represented in variable yoy2 per company per year. My calculation is calculating this growth per year, but doesn't look at the gvkey (unique company key).
For example,
while calculating the yoy growth for gvkey 2 in 2007, i want the result to be NA, since 2006 for this gvkey is unknown. This worked in the calculation of FJCC, but when I copy his code, I don't get the same result.
I do not have any good ideas about why the code would work for me and not for you. You can try the standard computer weirdness cure by exiting and restarting RStudio. Other than that, I am stumped.
I found out why it was not working at my Rstudio.
Apparently I had to install the package of dplyr and then use the library key to activate it.
If I only use the library function your method is not working.
Do you know what may be the root cause of this problem?
It seems something was wrong with your dpylr package. It was installed, otherwise library(dplyr) would have thrown an error, but group_by was not working correctly. Unless something similar happens again, I would put it down as a weird computer mystery and not worry about it.
OK, that is not how installing dplyr should work, so something is wrong. I do not know enough about how to debug such a problem to be helpful. I suggest you start a new thread with a a title like "Need to reinstall dplyr repeatedly", include a reprex, either the one you used in this thread or a simpler one taking the average of a few groups, and explain that the code only works after a fresh installation of dplyr. Mention whether group_by works until you terminate R, or if it fails after the first use, or whatever describes the situation. Sorry I can't help you more.