[R] About basic logical operators

William Dunlap wdunlap at tibco.com
Wed Mar 6 17:57:16 CET 2013


> > > x[x<0.2]
> >   [1] -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 -0.78

This is a bit off the original topic, but you should really put spaces around the "<".
Otherwise you might be surprised when you compare x to -0.2 instead of +0.2:
   > x<-seq(-1,1,by=0.02)
   > x[x<-0.2] 
   numeric(0)
   > x
   [1] 0.2


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Duncan Murdoch
> Sent: Wednesday, March 06, 2013 7:57 AM
> To: Victor hyk
> Cc: r-help at r-project.org
> Subject: Re: [R] About basic logical operators
> 
> On 05/03/2013 7:53 PM, Victor hyk wrote:
> > Hello everyone,
> >            I have a basic question regarding logical operators.
> > > x<-seq(-1,1,by=0.02)
> > > x
> >    [1] -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 -0.78
> >   [13] -0.76 -0.74 -0.72 -0.70 -0.68 -0.66 -0.64 -0.62 -0.60 -0.58 -0.56 -0.54
> >   [25] -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30
> >   [37] -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06
> >   [49] -0.04 -0.02  0.00  0.02  0.04  0.06  0.08  0.10  0.12  0.14  0.16  0.18
> >   [61]  0.20  0.22  0.24  0.26  0.28  0.30  0.32  0.34  0.36  0.38  0.40  0.42
> >   [73]  0.44  0.46  0.48  0.50  0.52  0.54  0.56  0.58  0.60  0.62  0.64  0.66
> >   [85]  0.68  0.70  0.72  0.74  0.76  0.78  0.80  0.82  0.84  0.86  0.88  0.90
> >   [97]  0.92  0.94  0.96  0.98  1.00
> > > x[x<=0.02]
> >   [1] -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 -0.78
> > [13] -0.76 -0.74 -0.72 -0.70 -0.68 -0.66 -0.64 -0.62 -0.60 -0.58 -0.56 -0.54
> > [25] -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30
> > [37] -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06
> > [49] -0.04 -0.02  0.00
> > > x[x<0.2]
> >   [1] -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 -0.78
> > [13] -0.76 -0.74 -0.72 -0.70 -0.68 -0.66 -0.64 -0.62 -0.60 -0.58 -0.56 -0.54
> > [25] -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30
> > [37] -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06
> > [49] -0.04 -0.02  0.00  0.02  0.04  0.06  0.08  0.10  0.12  0.14  0.16  0.18
> > [61]  0.20
> > >
> >       Why does "x[x<=0.02]" return  no 0.02
> 
> You don't have a 0.02 in your dataset.  Evaluate x[52] - 0.02 and you
> won't get zero due to rounding (as Jeff said, see FAQ 7.31).
> > but "x[x<0.2]" return a subsample with 0.02?
> 
> You don't have 0.2, either.  Evaluate x[61] - 0.2 and you get a negative
> value.
> 
> Duncan Murdoch
> 
> >       Anyone who can tell me why?
> >       Thanks!
> >
> >       Victor
> >
> > 	[[alternative HTML version deleted]]
> >
> >
> >
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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