[R] Simple control structure issue

Michael Ashton m.ashton at enduringinvestments.com
Sat Jun 24 00:18:30 CEST 2017


SONOFAGUN…I’m a bit embarrassed. Thanks Bill!

From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Friday, June 23, 2017 6:15 PM
To: Michael Ashton
Cc: r-help at r-project.org
Subject: Re: [R] Simple control structure issue

R is a case-sensitive language: 'if' (lowercase 'i') is a keyword and 'If' (uppercase 'I') is not.

Bill Dunlap
TIBCO Software
wdunlap tibco.com<http://tibco.com>

On Fri, Jun 23, 2017 at 1:00 PM, Michael Ashton <m.ashton at enduringinvestments.com<mailto:m.ashton at enduringinvestments.com>> wrote:
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]]

______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
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.


	[[alternative HTML version deleted]]



More information about the R-help mailing list