I want to calculate monthly returns for a time series of 4000 companies between 2014 and 2019.
This is how my dataset looks like
I'm using the following code to calculate the returns
nyseamex <- mutate(nyseamex, mon_return=adjprice
/lag(adjprice)-1)
So far so good. However looking at the data R calculates for every adjusted price the monthly return. This is getting a problem as soon as the company name changes:
I tried to group the names by using the function group_by() however the I got an error message when I run my function, please see below:
Does anyone know how to calculate the correct return for every single company in the dataset like having NA in the return column for the first entry of the new company and than calculating the return up to the last date and do the same procedure for every new company in the series?
Thanks in advance.