# 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

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