[R] else problem

Petr PIKAL petr.pikal at precheza.cz
Fri Jun 24 13:47:47 CEST 2011


Hi
> 
> Thank you for all your help! I did not know to use "" when searching for 

> help, as ?mean, etc, had always worked for me in the past. 
> It makes perfect sense why 'else' was causing me the trouble the way I 
was
> using it. I think it was working in my other code, despite the same 
> format, because it was part of a function and thus would have been 
> executed completely within the function?

Yes and no

if you made your function like that

fff<-function(x)
if (x>0) print("A")
else print("B")

you would get immediate error

> else {
Error: unexpected 'else' in "else"

So you probably enclosed whole function into brackets like

fff<-function(x) {
if (x>0) print("A")
else print("B")
}

and in that case you did not experienced this else problem not because it 
is a function but because you used those brackets.

Regards
Petr



> Cheers,
> Kara
> ________________________________________
> From: Bert Gunter [gunter.berton at gene.com]
> Sent: June 23, 2011 8:27 AM
> To: David Winsemius
> Cc: Kara Przeczek; r-help at r-project.org
> Subject: Re: [R] else problem
> 
> Perhaps some additional clarification...  (???)
> 
> >> if (length(dat2f$year)%%2==0) {
> >>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> >> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y)))) ]), 
med_TotQ =
> >> median(tot_km3y))]  }
> >> else {
> >
> > If this line is executed at a console session it will fail because the
> > interpreter does not keep a copy of the last condition. If you moved 
the
> > closing curley-brace to just befor the 'else', you should get the 
behavior
> > you expect (if your other code is correct):
> 
> Well, um.. not sure if this is what you meant, but what is happening
> at the console is that when you type <return>, the interpreter checks
> for a syntactically complete statement. If it finds what has been
> given to it **thus far** is, it tries to execute it (if not, it gives
> a continuation character and waits for more input) and, as you said,
> then starts anew to interpret the next line(s) entered, "forgetting"
> all previous. The problem above is that the" if()" statement up to the
> close bracket, "}" is syntactically complete, and so the "else{" that
> follows makes no sense as the beginnig of a new line to  be
> interpreted.
> 
> The simplest and universal solution to this is to simply enclose the
> whole conditional in" { }":
> 
> {if(length ...
> ...
> else {...}
> }
> 
> This forces the interpreter to wait for the last "}" before it will
> interpret and execute.
> 
> Hoping this clarifies rather than obfuscates.
> 
> -- Bert
> 
> 
> 
> >
> > Try instead:
> >
> > if (length(dat2f$year)%%2==0) {
> >  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> > median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y)))) ]), 
med_TotQ =
> > median(tot_km3y))]
> > } else {
> >  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> > med_TotQ = median(tot_km3y))]
> >  }
> >
> >
> >>  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> >> med_TotQ = median(tot_km3y))]
> >>  }
> >>
> >> Each individual piece works perfectly on its own, but together I get 
the
> >> following error:
> >>
> >>> if (length(dat2f$year)%%2==0) {
> >>
> >> +   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> >> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y)))) ]), 
med_TotQ =
> >> median(tot_km3y))] }
> >>>
> >>> else {
> >>
> >> Error: unexpected 'else' in "else"
> >>>
> >>>  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> >>> med_TotQ = median(tot_km3y))]
> >>>  }
> >>
> >> Error: unexpected '}' in "  }"
> >>>
> >>
> >> When I tried to look up "else" I got this error:
> >>
> >>> ?else
> >>
> >> Error: unexpected 'else' in "?else"
> >
> > Try instead:
> >
> > ?"else"
> >
> > --
> > David
> >>
> >> I have used exactly the same set up with if...else in other code and 
it
> >> worked fine then. I tried to run it again, and I got the same error 
as
> >> above. What is the problem? I hope it isn't something simple and 
silly!
> >>
> >> I realize that I can use the first line:
> >>
> >>  md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> >> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y)))) ]), 
med_TotQ =
> >> median(tot_km3y))]
> >>
> >> for all data sets and it will give me the median for both odd and
> >> even-length data sets, but it is now about the principle; why won't 
the
> >> if...else work?
> >>
> >> Thank you very much for your time!
> >>
> >> Kara
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> --
> "Men by nature long to get on to the ultimate truths, and will often
> be impatient with elementary studies or fight shy of them. If it were
> possible to reach the ultimate truths without the elementary studies
> usually prefixed to them, these would not be preparatory studies but
> superfluous diversions."
> 
> -- Maimonides (1135-1204)
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> ______________________________________________
> 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