How to ignore and not iterate over a for in R

Asked

Viewed 230 times

1

I am using this command to calculate 4 values extracted from a function summary without (sem_smry). 100 attempts are made where, if you report a mistake, the attempt is counted but not computed. I need to count the attempts "with error" but make sure that these errors are not stored in a matrix. At the end the matrix must contain 100 numerical values. Today the matrix stores the values obtained and, in the wrong attempts, it stores 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 stores the estimated values, when there is no 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 requested, I will explain the problem better. In the execution of the previous code, the result of the matrix 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 matrix keeps the values of NA, however, I would like the code not to keep these values and yes, to continue the execution of the loop, keeping the value of 11 in 10 and so on but still keeping the 100 expected executions for the iteration, the expected result is a matrix with the same 100 values as the previous one but only containing the numbers.

Previously, inside the IF do for I tried to get back the position of i, if a problem happened using i <= i - 1, to run the loop and go over the NA values when they happen, but I was unsuccessful.

  • The question is not very clear, and from what I understand, your code seems to work as you wish. If you want more detailed help, you will have to help us with your question, explaining in more detail the code objects and entering part of the data (dput(head(dados))).

  • 1

    Hello, I just edited the question, I hope that the problem has become clearer.

  • Try immediately after the } else { for if(is.na(n0[["RMSEA"]][1])) next.

1 answer

2

You can use the option while() in the sense that it will try several times until it does 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) 
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.