Less than negative?

September 6, 2010
By

(This article was first published on The Praise of Insects, and kindly contributed to R-bloggers)

An interesting thing that I found while doing the analysis for my previous post on the Canterbury earthquakes, was the difficulty of constraining figures less than a negative number. For example:

a<-seq(1, 10, 1)
b<-a[a>3 & a<7]
c<-seq(-1, -10, -1)
d<-c[c<-3 & c>-7] > a
[1] 1 2 3 4 5 6 7 8 9 10
> b
[1] 4 5 6
> c
[1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
> d
[1] -1 -2 -3 -4 -5 -6

The boolean operator for selecting a range less than a negative number ends up being the same as the assignment operator. To get around this I simply define the function neg()

neg<-function(x) -x
c<-seq(-1, -10, -1)
d<-c[c<neg(3) & c>neg(7)] > c
[1] -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
> d
[1] -4 -5 -6

To leave a comment for the author, please follow the link and comment on their blog: The Praise of Insects.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)