5

How can I compare a column in data frame for range? The range is like less than a negative number and greater than a positive number. For Positive number there is no problem but for negative number it is taking it as an assignment operator.Code for reference is given below

Resited<-Reap[mean < -5 & mean > 5,]
Kaushal
  • 61
  • 1
  • 1
  • 8
  • 4
    As long as you have a space between the `<` and the `-`, you should be fine. Can you give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) where you actually have trouble? With about being able to run the same code and get the same error it's difficult to help you. – MrFlick Dec 16 '14 at 20:40
  • 1
    Do you mean `Resited<-Reap[mean < -5 | mean > 5, ]`? Otherwise it doesn't make much sense – David Arenburg Dec 16 '14 at 21:00
  • This is either due to a typo or a complete misunderstanding of a logical-AND. Either way it should be closed as not reproducible or user-error (in thinking). – IRTFM Dec 16 '14 at 21:57
  • @DavidArenburg yes thats what i meant... – Kaushal Dec 17 '14 at 14:50
  • it keeps throwing the error "Error in mean < -5 : comparison (3) is possible only for atomic and list types" – Kaushal Dec 17 '14 at 14:51

3 Answers3

4

"mean" cannot be less than -5 and more than 5 at the same time. Did you mean logical OR? If both abs values are the same, you could simply write

Resited <- Reap[abs(mean) > 5, ]
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Alexey Ferapontov
  • 5,029
  • 4
  • 22
  • 39
3

This simplest way in my opinion is just to put parentheses around your negative value:

Resited<-Reap[mean<(-5) | mean>5,]
CephBirk
  • 6,422
  • 5
  • 56
  • 74
0

For the case where your range is not symmetric around zero, R allows you to define your own operators:

`%between%` = function(x,range) x>range[1] & x<range[2]
-6 %between% c(-7,-2)
[1] TRUE
crogg01
  • 2,446
  • 15
  • 35