I have a data table, my_data, with 3 variables, symbol, date, stock price and 141851 observations. The idea is to compute a Lag of 1 day on the stock price, taking care to group by symbol first, otherwise I will have lags computed using values from the previous stock in the data frame.
This are some rows from the table.
| Symbol | date | stock price |
|---|---|---|
| MSFT | 2019-02-12 | 106.8100 |
| MSFT | 2019-02-13 | 106.9000 |
| MSFT | 2019-02-14 | 108.2200 |
| APPL | 2018-01-02 | 168.9873 |
| APPL | 2018-01-03 | 168.9579 |
| APPL | 2018-01-04 | 169.7427 |
I need to to calculate the ratio {price(i)-price(i-1)} / price(i-1) for the whole table. But I cannot shift my it.
I started with R today, so I'm just struggling a lot.