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

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
  • 89,637
  • 17
  • 130
  • 188
Alexey Ferapontov
  • 4,779
  • 4
  • 19
  • 38
3

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

Resited<-Reap[mean<(-5) | mean>5,]
CephBirk
  • 5,994
  • 5
  • 54
  • 70
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
Hans Roggeman
  • 2,402
  • 14
  • 34