Hi everybody,
I've found a very interesting problem in R: the if-else statement doesn't
work in a main program. Sounds crazy, but true.
I tried this very easy example, and I got syntax error at the "else" line.
Example:
-------
a <- 1
if ( a == 1 )
print("yes")
else
print("no")
--------
I tried on Windows and on Linux, but none of them works.
(Detailed version info at the end of the mail.)
Temporally you could fix it in two way:
1. Put it in 1 line:
if ( a == 1) print("yes") else print("no")
will work.
2. Put it in a function.
If you have any idea for better resolve of this problem,
please send me an email, I'm not subscribed to the list.
Thanks,
Anita
Version info:
1. Windows:
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 1
minor 5.0
year 2002
month 04
day 29
language R
2. Linux:
platform i686-pc-linux-gnu
arch i686
os linux-gnu
system i686, linux-gnu
status
major 1
minor 5.1
year 2002
month 06
day 17
language R
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
R bug? (if-else problem in main program)
5 messages · Anita Gulyasne Goldpergel, Lorenz Gygax, Peter Dalgaard +1 more
Example:
-------
a <- 1
if ( a == 1 )
print("yes")
else
print("no")
--------
If you want to split this over several lines, you need curly braces:
a <- 1
if ( a == 1 ) {
print ('yes')
} else {
print ('no')
}
Cheers, Lorenz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Lorenz Gygax" <gygax at ifi.unizh.ch> writes:
If you want to split this over several lines, you need curly braces:
a <- 1
if ( a == 1 ) {
print ('yes')
} else {
print ('no')
}
Yep. And it isn't a bug, but a design issue. In interactive mode we print the result of an expression as soon as it is syntactically complete, so upon seeing
a <- 1 ; if ( a == 1 ) print("yes")
[1] "yes" we assume that we are done with that statement. After all, the next line might be completely unrelated, e.g. "b <- 2", so we cannot wait and see whether it starts with "else". So you need to ensure somehow that the line is not syntactically complete, using braces as above, or, e.g.,
a <- 1 ; if ( a == 1 ) print("yes") else
+ print("no")
[1] "yes"
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This is documented in many places. See the help page for `if', or in
fuller detail, section 3.2 of the Language Manual.
The solution is to use braces
if(a == 1) {
print("yes")
} else {
print("no")
}
as described in ?"if".
On Tue, 5 Nov 2002, Anita Gulyasne Goldpergel wrote:
I've found a very interesting problem in R: the if-else statement doesn't work in a main program. Sounds crazy, but true.
This is not `a main program' but using R interactively at the top (prompt) level. It will work in a function, the only sort of `programs' that exist in R. The help and manuals are your friends! [...]
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thank you very much everybody for your answers!
Before I wrote my message, I had tried to use
brackets. So I've discovered something, what can be
useful for everyone:
If I put the end-bracelet in different line from else,
it doesn't work:
if ( a == 1 ) {
print("yes")
}
else {
print("no")
}
But if I put it one line, as you wrote, like } else {
it works.
Thanks again,
Anita
Lorenz Gygax writes:
Example:
-------
a <- 1
if ( a == 1 )
print("yes")
else
print("no")
--------
If you want to split this over several lines, you need curly braces:
a <- 1
if ( a == 1 ) {
print ('yes')
} else {
print ('no')
}
Cheers, Lorenz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._