1

I know there is an answer for how to break out of a for loop, but I'm still having trouble breaking out of a for loop when a condition is met, and then continuing onto the expression in the outer loop:

x <- 1:100
n <- as.factor(rep(c(1:5), 20))
data <- data.frame(x, n)
y <- 1:20
m <- 10

for(i in levels(n)){
  x.subset <- subset(data$x, data$n == i)

  for(j in 1:length(y)){
    x.big <- NULL
    x.big[j] <- x.subset[j]^2
    x.big.prev <- x.big #####

    if((mean(x.big.prev)-mean(x.big)) >=30){
          break
        } #end of if() statement
      } #end of j-loop
      plot(y, x.big.prev)
} #end of i loop

I'm trying to get the j loop to stop when the new x.subset (with a new set of values from i) has a greater mean than the previous x.subet by 30. I attempted that by comparing x.big.prev with x.big. I either keep getting one of two outcomes:

Error in if ((mean(x.big.prev) - mean(x.big)) >= 30) { : 
missing value where TRUE/FALSE needed

OR

It runs through all the iterations of j and just plots everything.

I think I put the expression x.big.prev <- x.big in the wrong place, but then again, I'm not sure where to put to allow for a comparison between the means of the two vectors.

Any ideas?

Community
  • 1
  • 1
Lalochezia
  • 497
  • 4
  • 15
  • You need to put `x.big.prev <- x.big` after the `if` condition and initialize `x.big.prev` outside the inner loop. PS: `x.subset <- subset(data, n == i, select = x)` is the correct syntax. Of course `data[data$n == i, "x"]` is usually preferable. – Roland Dec 01 '16 at 09:33
  • A minor thing (and maybe only applicable in your example): When you define `y <- 1:20`, `for(j in y)` will be the same as `for(j in 1:length(y)`. – LAP Dec 01 '16 at 09:36
  • @Roland, even with his/her setup at this time, the `mean(x.big.prev)-mean(x.big)` should result in `0` and not produce an error message when used in the `if()`-statement, or should it? – LAP Dec 01 '16 at 09:41
  • 1
    Your problem is the `x.big <- NULL`, which produces `NA` after the first loop for all values before the actual, because in `x.big[j] <- x.subset[j]^2` with `j > 1`, there are elements that are not defined. – LAP Dec 01 '16 at 09:44
  • 1
    Just to expand what @LeoP. said, try on the console `x.big<-NULL`, then set an element to a value (for instance `x.big[7]<-10`) and see how `x.big` has changed. Try also `mean(x.big)` and see what it returns. – nicola Dec 01 '16 at 09:47
  • Thank you everyone for your comments and answered, I managed to make it work! – Lalochezia Dec 01 '16 at 15:58

1 Answers1

1

You need to initialize x.big and x.big.prev outside of the inner loop. Also the redefining of x.big.prev inside the inner loop has to be done after the if()-statement. Try this:

for(i in levels(n)){
  x.subset <- subset(data$x, data$n == i)
  x.big <- NULL
  x.big.prev <- 1

  for(j in y){
    x.big[j] <- x.subset[j]^2

    if((mean(x.big.prev)-mean(x.big)) >=30){
          break
        } #end of if() statement
    x.big.prev <- x.big #####
      } #end of j-loop
      plot(y, x.big.prev)
} #end of i loop
LAP
  • 6,605
  • 2
  • 15
  • 28