[R] wrong if-else syntax

Duncan Murdoch murdoch at stats.uwo.ca
Mon May 4 13:04:31 CEST 2009


On 04/05/2009 5:08 AM, mauede at alice.it wrote:
> What is wrong in the following nested if-else statements:
> 
>   if (Condition_1) {              # begin IF_1                  
>      statement_1
>      statement_2
>      statement_3
>      if (Condition_2) {                 # begin IF_2
>         a<- a +1
>      }                                       # end IF_2
>      statement_4  
>      statement_5
>      statement_6
>      statement_7
>      if (Condition_3) {                # begin IF_2                                                 
>         statement_8
>      } else {                             # ELSE_2
>         statement_9
>         statement_10
>      }                                      # end IF_2
>   } else {                           # ELSE_1
>       statement_11
>   }                                   # end IF_1
> 
> 
> It looks like R interpret does not like the above syntax. In fact in my script I have the following instructions:
> 
>   if (DonohoAplhaON){                                
>       tms <<- xx[,"sampamp"]                               #EXTRACT SIGNAL AMPLITUDE
>       tmsLen <<- length(tms)
>       J <<- ilogb(tmsLen, base=2)
>       if (logb(tmsLen, base=2)%%2 > 0) {
>          J <<- J + 1
>       }
>       rm(xx)
>       rawtms <- tms 
>       X <- PreProcessor(tms,tmsLen,J)
>       BestWavList <- FindBestWavelet (X,tmsLen,J,Step1NumHighScalesOFF)
>       if (!is.null(BestWavList)) {                                                         #COMPUTE SIGNAL DONOHO-ALPHA
>          features.mat[ns,"alpha"]<- CalcDonohoAlpha(rawtms,BestWavList$No,J,tmsLen,Step3NumHighScalesOFF,Step3AllCoefON)
>       } else {
>          cat("\n\n Could not compute Best Wavelet Basis for Signal: ",fln,"  Skip current signal! \n\n")
>          next
>      }
>   } else {
>       features.mat[ns,"alpha"] <- NA
>   }
> 
> It keeps printing out an error referred to the second " } else {" as if it could not realize that the inner if-statement has been closed:

I just tried the full block above, and it was fine.

> 
>>      if(!is.null(BestWavList)) {                                                         #COMPUTE SIGNAL DONOHO-ALPHA

Notice the prompt here is >, not +.  The problem occurred earlier:  R 
thinks this is a new statement, not part of the "if (DonohoAplhaON) {" 
block as you intended.

> +         features.mat[ns,"alpha"]<- CalcDonohoAlpha(rawtms,BestWavList$No,J,tmsLen,Step3NumHighScalesOFF,Step3AllCoefON)
> +      }else {
> +         cat("\n\n Could not compute Best Wavelet Basis for Signal: ",fln,"  Skip current signal! \n\n")
> +         next
> +      }

This finishes the statement, so the else doesn't make sense.

>>   }else {

The spacing here doesn't match what you showed above.  Are you sure 
you're pasting the right file?

You'll get better error handling using source() than cut and paste. 
source() will stop at the first error; cut and paste just keeps going.

Duncan Murdoch


> Error: unexpected '}' in "  }"
>>      features.mat[ns,"alpha"] <- NA
>>   }
> Error: unexpected '}' in "  }"
> 
> 
> Thank you for clarifying my doubts
> Maura
> 
> 
> 
> 
> tutti i telefonini TIM!
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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