[R] Fwd: how to calculate the return?

Denver XU financialengine at gmail.com
Sat Nov 24 15:51:27 CET 2007


hi,scionforbai,
Thank you very much.
I made very stupid mistake for the number is not stock price.
In fact, if the number is correct, data[i,1] is equivalent to data[i]



2007/11/24, Scionforbai <scionforbai at gmail.com>:
> Hi Denver,
>
> > I want  to calculate the return say AMR,so I use
>
> If data is a matrix  or a data.frame you need to use the correct
> index: [row,col] instead of [row]. Try:
>
> re=numeric(10)
> for (i in 2:nrow(data)) {
>      re[1]=0
>      re[i]=log(data[i,1]/data[i-1,1])
> }
>
> This works, but of course you have negative values of the ratio, so
> the log cannot be computed.
> See also ?apply.
>



More information about the R-help mailing list