[R] Simple control structure issue

Michael Ashton m.ashton at enduringinvestments.com
Fri Jun 23 22:00:58 CEST 2017


I am having a hard time with 'next'. I come from the "sloppy" school that learned BASIC with Goto.

Conceptually next seems pretty straightforward. I just can't get it to work correctly in my code. Here's a stripped down version:

WhichRunNow<-"Daily"
Cnums=c(0,1,"2b3")
Cpers=c("Daily","Daily","Weekly")

for (j in (1:length(Cnums))) {
     If (!identical(Cpers[j],WhichRunNow)){
           next
           }
     print(j)
}

So, this should print the number "3", which it does. It prints 3 because "WhichRunNow" is "Daily", and only for j=3 is Cpers[j] not identical to "Daily". But R seems very unhappy with how it gets there:

> WhichRunNow<-"Daily"
> Cnums=c(0,1,"2b3")
> Cpers=c("Daily","Daily","Weekly")
>
> for (j in (1:length(Cnums))) {
+ If (!identical(Cpers[j],WhichRunNow)){
Error: unexpected '{' in:
"for (j in (1:length(Cnums))) {
If (!identical(Cpers[j],WhichRunNow)){"
> next
Error: no loop for break/next, jumping to top level
> }
Error: unexpected '}' in "}"
> print(j)
[1] 3
> }
Error: unexpected '}' in "}"
>

Obviously something with my syntax, but I can't see what I'm doing wrong!


	[[alternative HTML version deleted]]



More information about the R-help mailing list