[R] if syntax error :(

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 14 00:45:04 CEST 2006


On 6/13/06, H. Paul Benton <hpbenton at scripps.edu> wrote:
> Thank you,
> That was the problem. I know about using the extra brackets but in the R
> help manual it doesn't use brackets so I thought maybe R is different.

The problem is not that you must use brackets -- you don't.  The problem is that
you put the else INSIDE the brackets.

> However, yes indeed, it was assigning the 'lgAB to 4'. I'm just starting to
> really learn R.
> Cheers,
>
> Paul
>
> PS thanks to the Patrick Burns who gave me the link to S poetry.
> PPS What is the "sturges" doing in the code ? is it like a goto?

Look at ?hist.default to see that "Sturges" is the default value for breaks.


> <
> freq_AB <-  hist(lgAB,  type = "o", plot = FALSE,
> breaks = if (any(abs(lgAB) > 4)) "Sturges" else br)
> >
> Research Techinician
> Mass Spectrometry
>  o The
>  /
> o Scripps
>  \
>  o Research
>  /
> o Institute
>
>
> -----Original Message-----
> From: Rolf Turner [mailto:rolf at erdos.math.unb.ca]
> Sent: Tuesday, June 13, 2006 1:14 PM
> To: hpbenton at scripps.edu; r-help at stat.math.ethz.ch
> Subject: Re: [R] if syntax error :(
>
> H. Paul Benton wrote:
>
> > Umm sorry to bother everyone again but I'm having trouble with my if
> > statement. I come from a perl background so that's probably my problem! :)
> > So here is my code:
> >
> > if (any(lgAB>4) | any(lgAB<-4)){
> >       freq_AB<-hist(lgAB, type="o", plot=F)
> >       else
> >       freq_AB<-hist(lgAB, breaks=br,type ="o", plot=F)
> > }
> >
> > And I get
> > > source("E:/R/GMDA-1.1.R")
> > Error in parse(file, n = -1, NULL, "?") : syntax error at
> > 11:             freq_AB<-hist(lgAB, type="o", plot=F)
> > 12:             else
> > >
>
> No-one yet has pointed out the following problem, which, while not be
> a syntax error as such, will cause you headaches:
>
>        if (any(lgAB>4) | any(lgAB<-4)){
>                              ^^^^^^^
>
> This assigns the value 4 to lgAB (which is presumably NOT what
> you want to do).  You want ``any(lgAB < -4)''.
>
> General rule:  Put in spaces around operators --- it makes the
> code (much) more readable and avoids unintended consequences.
>
> Another infelicity in your code: ``plot=F''.  Use ``plot=FALSE''.
> (Note that the symbols ``F'' and ``T'' are assignable, *unlike*
> ``TRUE'' and ``FALSE''.)
>
>                                cheers,
>
>                                        Rolf Turner
>                                        rolf at math.unb.ca
>
>
>
>



More information about the R-help mailing list