How to ignore and not iterate over a for in R

1

I am using this command to compute 4 values extracted from a function summary without (sem_smry). 100 attempts are made where, if it reports an error, the attempt is counted but not computed. I need to count the "failed" attempts but do not get these errors stored in an array. At the end the array must contain 100 numeric values. Today the matrix stores the values obtained and, in the wrong attempts, saves NA. How can I skip the wrong attempts, but count those mistakes. The code below is the part of the program that counts the wrong attempts and saves the estimated values when it does not give error.

  for (j in 1:length(pp)) {

  for (i in 1:100) {
    n0 <- sem_smry(dados, cfa, pp[j])
    if (any(class(n0) == "try-error")) {
      nnc[j] <- nnc[j] + 1
    } else  {
      RMSEA[i]<- n0[["RMSEA"]][1]
      NFI[i]<-n0[["NFI"]]
      GFI[i]<-n0[["GFI"]]
      CFI[i]<-n0[["CFI"]]
    }

    Indices <- cbind(RMSEA, GFI, NFI, CFI) 
}

[EDIT] As a request, I will explain the problem better. In the previous code execution, the result of the array is as follows:

  [1,] 0.12487090 0.7439197 0.8193802 0.9197872
  [2,] 0.05692412 0.8101720 0.8266571 0.9755843
  [3,] 0.11701959 0.7300448 0.7942616 0.9129401
  [4,] 0.10471572 0.7821648 0.8551393 0.9498845
  [5,] 0.09479998 0.7788558 0.8503112 0.9544811
  [6,] 0.13484758 0.7301779 0.7921450 0.8972345
  [7,] 0.12166489 0.7644818 0.8204229 0.9226289
  [8,] 0.10654332 0.7783956 0.8157848 0.9319917
  [9,] 0.19616764 0.6322465 0.6992935 0.8351372
 [10,]         NA        NA        NA        NA
 [11,] 0.17654647 0.6631480 0.7123008 0.8594085
 ...

As you can see, on line 10, the array stores the values of NA, however, I would like the code not to keep those values, but to continue executing the loop, saving the value of 11 in 10 and so on. while still maintaining the 100 executions expected for the iteration, the expected result is an array with the same 100 values as above but only containing only the numbers.

Previously, within the IF of for I tried to return to the position of i, if a problem occurred using i

asked by anonymous 30.08.2018 / 16:13

1 answer

2

You can use the while() option in the sense that you will try several times until you do not get an error, keeping the value of i fixed:

for (j in 1:length(pp)) {

  for (i in 1:100) {

    n0 <- sem_smry(dados, cfa, pp[j])

    while (any(class(n0) == "try-error")) {
      nnc[j] <- nnc[j] + 1
      n0 <- sem_smry(dados, cfa, pp[j])
    }

    RMSEA[i]<- n0[["RMSEA"]][1]
    NFI[i]<-n0[["NFI"]]
    GFI[i]<-n0[["GFI"]]
    CFI[i]<-n0[["CFI"]]

    Indices <- cbind(RMSEA, GFI, NFI, CFI) 
  }
}
    
30.08.2018 / 18:26