Reputation: 25
I'm just starting to get to know R, and I know how to calculate the returns for a single column simply with ((new-old)/old) or with Delt from the quantmod package. However, I have to calculate the returns for each column and create new columns that contain this return. My data is structured in the following way: enter image description here
Is there a quick way to do this?
Thanks!
Upvotes: 0
Views: 44
Reputation: 581
Yes there is a function! You are searching for apply
. With apply you can repeat a function for every row or column in a matrix for example.
In your case it should look like
apply(data,2,function)
data and function is self explanatory. If you insert a "2" it is done by column, "1" for by row.
Upvotes: 1