[R] for loop output

Sarah Goslee sarah.goslee at gmail.com
Wed Oct 17 21:53:20 CEST 2012


At the end of the loop, the last output should be stored in int, so

int

will give it to you. You don't need the print(int) within the loop
unless you like to verify that the loop is working.

Sarah

On Wed, Oct 17, 2012 at 2:16 PM, stats12 <skarmv at gmail.com> wrote:
> Dear R users,
>
> In the code below, I am trying to print the result of my loop function. The
> output first gives me the result for k=1, and then for k=1 and k=2. I only
> want the last output which is
>
>       [,1]      [,2]
>  [1,] 0.1700065 0.5002659
>  [2,] 0.3080273 0.4954731
>  [3,] 0.4844886 0.4544306
>  [4,] 0.5062987 0.1868154
>  [5,] 0.5846982 0.4353522
>  [6,] 0.4332621 0.2202922
>  [7,] 0.4391985 0.2424147
>  [8,] 0.4242643 0.2372497
>  [9,] 0.3274367 0.3664741
> [10,] 0.1937940 0.4192495
>
> How can I extract only this part of my output. Thank you very much in
> advance.
>
>
>
> d<-matrix(c(1,1,0,0,0,0,0,0,2,1,0,0,1,1,0,1,2,2,1,0),nrow=10,ncol=2)
>
> h<-matrix(runif(20,0,1),10)
>
> int<-matrix(c(0),nrow=10, ncol=2)
> for (k in 1:2){
>  for(s in 1:10){
>
> integrand<-function(x) x^d[s,k]*exp(-x*gamma(1+1/1.6))^1.6*exp(-x*h[s,k])
> integ[s,k]<-quadgk(integrand,0,1000)
> }
> print(int)
>  }
>
>            [,1] [,2]
>  [1,] 0.1700065    0
>  [2,] 0.3080273    0
>  [3,] 0.4844886    0
>  [4,] 0.5062987    0
>  [5,] 0.5846982    0
>  [6,] 0.4332621    0
>  [7,] 0.4391985    0
>  [8,] 0.4242643    0
>  [9,] 0.3274367    0
> [10,] 0.1937940    0
>            [,1]      [,2]
>  [1,] 0.1700065 0.5002659
>  [2,] 0.3080273 0.4954731
>  [3,] 0.4844886 0.4544306
>  [4,] 0.5062987 0.1868154
>  [5,] 0.5846982 0.4353522
>  [6,] 0.4332621 0.2202922
>  [7,] 0.4391985 0.2424147
>  [8,] 0.4242643 0.2372497
>  [9,] 0.3274367 0.3664741
> [10,] 0.1937940 0.4192495
>
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list