[R] Question Regarding a nested for loop in R

MacQueen, Don macqueen1 at llnl.gov
Fri Dec 4 17:20:08 CET 2015


I'll hope this isn't homework (R-help has a "don't do people's homework
for them" convention), and make a few suggestions.

The inner loop is not needed. For example, you can replace

for (i in 1:15 ) {


Inv1Outcome[i] = if (random[i] <= .25){Inv1Returns[1]}
  else if (random[i] > .25 & random[i] <= .50){Inv1Returns[2]}
  else if (random[i] > .50 & random[i] <= .75){Inv1Returns[3]}
  else {Inv1Returns[4]}

}


with

 
Inv1Outcome <- rep(Inv1Returns[1],15]

Inv1Outcome[random > 0.25 & random <= 0.50] <- Inv1Returns[2]
Inv1Outcome[random > 0.50 & random <= 0.75] <- Inv1Returns[3]
Inv1Outcome[random > 0.75]                  <- Inv1Returns[4]

And similarly for the other two outcomes.

For that matter, if I interpret correctly what's going on, this might do
it:

Inv1Outcome <- sample(Inv1Returns, 15, replace=TRUE, prob=c(0.25, 0.25,
0.25, 0.25)



I don't think I understand what you're trying to do with the outer loop,
but it does occur to me that with 'random' defined outside both loops, the
inner loop will have exactly the same results all four times, and does
that make any sense? Try putting
  random = runif(15, 0, 1)
inside the outer loop but outside the inner loop.

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 12/3/15, 2:42 PM, "R-help on behalf of Vermilio, Ryan"
<r-help-bounces at r-project.org on behalf of RYAN.VERMILIO at ucdenver.edu>
wrote:

>Hello there,
>
>I'm an R novice and am trying to figure out how to create an external for
>loop.  My current loop iterates 15 times and stores the resulting values
>in 3 separate vectors.  What I need is to create an outside loop that
>will run internal loop 4 times, sum the resulting vectors for each, and
>then store the sum of each in a vector of length 4 for each investment.
>
>Right now, the target vector has the same value in each position, which
>is what I'm trying to fix.
>
>Here's what I have at this point:
>
>Inv1Returns <- c(0, 1000, -500, 500)
>Inv2Returns <- c(0, -9000, 30000, 10000)
>Inv3Returns <- c(0, 4000, -1000, -2000)
>
>random = runif(15, 0, 1)
>
>Inv1Outcome = NULL
>Inv2Outcome = NULL
>Inv3Outcome = NULL
>
>Inv1Total = NULL
>Inv2Total = NULL
>Inv3Total = NULL
>
>for (j in 1:4)
>
>{
>
>for (i in 1:15 )
>
>{
>
>  Inv1Outcome[i] = if (random[i] <= .25){Inv1Returns[1]}
>  else if (random[i] > .25 & random[i] <= .50){Inv1Returns[2]}
>  else if (random[i] > .50 & random[i] <= .75){Inv1Returns[3]}
>  else {Inv1Returns[4]}
>
>  Inv2Outcome[i] = if (random[i] <= .20){Inv2Returns[1]}
>  else if (random[i] > .20 & random[i] <= .30){Inv2Returns[2]}
>  else if (random[i] > .30 & random[i] <= .70){Inv2Returns[3]}
>  else {Inv2Returns[4]}
>
>  Inv3Outcome[i] = if (random[i] <= .50){Inv3Returns[1]}
>  else if (random[i] > .50 & random[i] <= .70){Inv3Returns[2]}
>  else if (random[i] > .70 & random[i] <= .90){Inv3Returns[3]}
>  else {Inv3Returns[4]}
>
>}
>
>Inv1Total = append(Inv1Total, sum(Inv1Outcome))
>Inv2Total = append(Inv2Total, sum(Inv2Outcome))
>Inv3Total = append(Inv3Total, sum(Inv3Outcome))
>
>}
>
>Inv1Total
>Inv2Total
>Inv3Total
>
>
>Sincerely,
>
>Ryan
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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