[R] Re: R bug? (if-else problem in main program)

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Nov 7 08:40:36 CET 2002


I previously posted that this *is all* documented in the R Language
manual. Rather than trying to deduce the syntax of R by trial-and-error,
would it not be easier to read the documentation?

On Wed, 6 Nov 2002, Li, Fasheng wrote:

> I found the same thing.  But if you write the same code in a function, there
> is no problem. E.g.,
>
> myfunc<-function(a)
> {
>   if ( a == 1 ) {
>    print("yes")
>   }
>   else {
>    print("no")
>   }
> }
>
> # Or
>
> myfunc<-function(a)
> {
>   if ( a == 1 )
>    print("yes")
>   else
>    print("no")
> }
>
> > myfunc(5)
> [1] "no"
> > myfunc(1)
> [1] "yes"
>
> Therefore, it may be a good idea to write functions for specific tasks from
> programming point of view.
>
> Hope this is also helpful.
>
> -Fasheng
>
> -----Original Message-----
> From: Anita Gulyasne Goldpergel [mailto:anitag at latte.harvard.edu]
> Sent: Wednesday, November 06, 2002 2:22 PM
> To: Lorenz Gygax
> Cc: r-help at stat.math.ethz.ch
> Subject: [R] Re: R bug? (if-else problem in main program)
>
>
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._
>
>
> LEGAL NOTICE
> Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list