[R] how to calculate the return?
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Nov 24 15:59:50 CET 2007
Are you sure its a matrix? or is it a data frame?
> BOD # built into R
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
> class(BOD)
[1] "data.frame"
> BOD[2]
demand
1 8.3
2 10.3
3 19.0
4 16.0
5 15.6
6 19.8
> BOD[[2]]
[1] 8.3 10.3 19.0 16.0 15.6 19.8
> BOD[,2]
[1] 8.3 10.3 19.0 16.0 15.6 19.8
> bod <- as.matrix(BOD)
> class(bod)
[1] "matrix"
> bod[2]
[1] 2
> bod[,2]
[1] 8.3 10.3 19.0 16.0 15.6 19.8
You may wish to review the Introduction to R manual and the many contributed
introductions at the first hit found by googling for
CRAN contributed documentation
On Nov 24, 2007 9:07 AM, Denver XU <financialengine at gmail.com> wrote:
> Hi, R-users,
> data is a matrix like this
> AMR BS GE HR MO UK SP500
> 1974 -0.3505 -0.1154 -0.4246 -0.2107 -0.0758 0.2331 -0.2647
> 1975 0.7083 0.2472 0.3719 0.2227 0.0213 0.3569 0.3720
> 1976 0.7329 0.3665 0.2550 0.5815 0.1276 0.0781 0.2384
> 1977 -0.2034 -0.4271 -0.0490 -0.0938 0.0712 -0.2721 -0.0718
> 1978 0.1663 -0.0452 -0.0573 0.2751 0.1372 -0.1346 0.0656
> 1979 -0.2659 0.0158 0.0898 0.0793 0.0215 0.2254 0.1844
> 1980 0.0124 0.4751 0.3350 -0.1894 0.2002 0.3657 0.3242
> 1981 -0.0264 -0.2042 -0.0275 -0.7427 0.0913 0.0479 -0.0491
> 1982 1.0642 -0.1493 0.6968 -0.2615 0.2243 0.0456 0.2141
> 1983 0.1942 0.3680 0.3110 1.8682 0.2066 0.2640 0.2251
> I want to calculate the return say AMR,so I use
> re=numeric(10)
> for (i in 2:nrow(data))
> re[1]=0
> re[i]=log(data[i]/data[i-1])
> to my surprise, the result is
> > re
> [1] 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
> [9] 0.00000 -1.70109
> I don't know what's wrong with my code, and is there alternative way
> to do the same?
> thanks
>
> Denver
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list