Less than negative?

[This article was first published on The Praise of Insects, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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[cneg(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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)