Hi All,

I'm an Excel guy trying to convert over to R for my investment analysis. Here is where I'm stuck (random data below... not actual performance numbers):

|date |voo1_return|agg1_return|tsla1_return|voo5_return|agg5_return|tsla5_return|

|2020-08-15| (0.00808) | (0.01336) | (0.00402) | 0.07177 | 0.06700 | 0.01008 |

|2020-08-16| 0.01370 | 0.01037 | 0.01946 | 0.08099 | 0.06400 | 0.02741 |

I have a data frame with dates in the first column and different stock returns over different trailing periods in each column thereafter (rows may be the appropriate semantics?). Within my data frame, I want to add a column called "max1_return" that is based on a logic:

If the lag of the "5_return" from the tickers above is greater than the lag of the "voo5_return", then I want the corresponding (not lagged) "1_return". If the lag of the "5_return" is less than "voo5_return", then I want to know which lag "5_return" is greatest, and use that corresponding ticker (not lagged) for the "1_return".

Using the data I typed above as an example, my new column should be labeled "max1_return" and the results should be 0.01370 on 8/16 because the voo5_return from 8/15 was the largest.

|date |voo1_return|agg1_return|tsla1_return|voo5_return|agg5_return|tsla5_return|max1_return|

|2020-08-15| (0.00808) | (0.01336) | (0.00402) | 0.07177 | 0.06700 | 0.01008 |

|2020-08-16| 0.01370 | 0.01037 | 0.01946 | 0.08099 | 0.06400 | 0.02741 |**0.1370**

I run into trouble writing the correct formula or embedding it into the mutate function. Any insight would be greatly appreciated!

Thank you!

- Jeremy