[R] CORRECTION - Storing results in a loop

jim holtman jholtman at gmail.com
Mon Feb 15 14:35:57 CET 2010


You might want to understand loops a little more; you are modifying
'j' within the loop which does not do whatever you think it should be
doing.  You also probably want "B" to be a matrix if you want to store
the results for everything besides the last iteration.

Here is your loop corrected and followed by a vectorized way of doing
it without loops:

> ONS <- data.frame(replicate(7, 1:10))
>
> n = length(ONS)
> Y = NULL
>
> B = matrix(nrow=nrow(ONS) - 1, ncol=length(ONS))
>
>
> for (i in 1 : n)
+  {
+  Y[i] = ONS[i]
+
+ #--  j     <- 1  Removed -- not needed. why is it here?
+  for (j in 1:(length(Y[[i]])-1))
+        {
+   B[j, i]     <- log((Y[[i]][j+1])/(Y[[i]][j]))
+
+
+ #-- j <- j+1   Remove -- why is it here?
+
+
+  }
+
+  }
>  B  # print B
           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
 [1,] 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472
 [2,] 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651
 [3,] 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821
 [4,] 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436
 [5,] 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216
 [6,] 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507
 [7,] 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314
 [8,] 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830
 [9,] 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605
>
> # now using 'vectorized' operations; should have same values
> sapply(ONS, function(.col){
+     log(tail(.col, -1) / head(.col, -1))
+ })
             X1        X2        X3        X4        X5        X6        X7
 [1,] 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472 0.6931472
 [2,] 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651 0.4054651
 [3,] 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821 0.2876821
 [4,] 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436 0.2231436
 [5,] 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216 0.1823216
 [6,] 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507 0.1541507
 [7,] 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314 0.1335314
 [8,] 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830 0.1177830
 [9,] 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605 0.1053605
>
>


On Mon, Feb 15, 2010 at 7:41 AM, Madhavi Bhave <madhavi_bhave at yahoo.com> wrote:
> Dear Sir,
>
> Thanks a lot for your quick solution. But still I will like someone to guide me the solution as per my requirement. The problem is actually I am not looking for the square of each term. I have used it to give some example since I didn't want to confuse the matter. My process is altogether different and I need to use loop. So here is my actual code. I am trying to use Historical simulation and for that I need to calculate LN(New rate / old rate for each of the instrument separately).
>
> Here is my actual code -
>
> ONS = read.csv('Instrument.csv')
> n = length(ONS)
> Y = NULL
> B = array()
>
> for (i in 1 : n)
>  {
>  Y[i] = ONS[i]
>   j     <- 1
>  for (j in 1:(length(Y[[i]])-1))
>        {
>   B[j]     <- log((Y[[i]][j+1])/(Y[[i]][j]))
>
>
>  j <- j+1
>
>
>  }
>
>  }
>
>
>
>
> --- On Mon, 15/2/10, Benilton Carvalho <beniltoncarvalho at gmail.com> wrote:
>
>
> From: Benilton Carvalho <beniltoncarvalho at gmail.com>
> Subject: Re: [R] CORRECTION - Storing results in a loop
> To: "Madhavi Bhave" <madhavi_bhave at yahoo.com>
> Cc: r-help at r-project.org
> Date: Monday, 15 February, 2010, 4:29 AM
>
>
> sorry, meant to type:
>
> B = ONS^2
>
> cheers,
> benilton
>
> On Mon, Feb 15, 2010 at 12:28 PM, Benilton Carvalho
> <beniltoncarvalho at gmail.com> wrote:
>> maybe you just want
>>
>> Y = ONS^2
>>
>> ?
>>
>> b
>>
>> On Mon, Feb 15, 2010 at 12:22 PM, Madhavi Bhave <madhavi_bhave at yahoo.com> wrote:
>>> Dear R Helpers
>>>
>>> (There is a small correction in my earlier mail. In the 'instrument.csv' file, I had mentioned only three columns. Actually there are 7 columns. I regret the error. Rest contents remains the same. Thanks)
>>>
>>> I have an 'instrument.csv' file with 7 instrument names and 5 rates each i.e. it has 7 columns and 6 rows (including row names).
>>>
>>> 'instrument.csv'
>>>
>>> instrument1      instrument2   ........     instrument7
>>> 12                         5                              14
>>> 11                         7                                7
>>> 14                       11                                3
>>>   8                       21                              10
>>> 11                         3                                5
>>>
>>>
>>> Following is my R code.
>>>
>>> ONS = read.csv('Instrument.csv')
>>> n = length(ONS)
>>>
>>> Y = NULL
>>> B = NULL
>>>
>>> for (i in 1 : n)
>>>
>>>  {
>>>
>>>  Y[i] = ONS[i]
>>>
>>>   for (j in 1 : length(Y[[i]]))
>>>    {
>>>    B[j] = (Y[[i]][j])^2
>>>    }
>>>
>>>  }
>>>
>>> Problem is when I type B, I get the processed result only for the last column i.e. Y[7]. It doesn't store results for Y[1] to Y[7].
>>>
>>> I need B[1], B[2].......upto B[7].
>>>
>>> Please guide me how do I store individual column processed results?
>>>
>>> Thanking you all in advance
>>>
>>> Regards
>>>
>>> Madhavi
>>>
>>>
>>>
>>>
>>>      The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
>>>        [[alternative HTML version deleted]]
>>>
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>
>
>
>      Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list