Maurits
Maurits

Reputation: 25

Is there a function to calculate the returns on a dataset of many (997) different stocks?

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

Answers (1)

Triss
Triss

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

Related Questions