Stefan
Stefan

Reputation: 895

Filter or subset based on NOT greater than AND NOT smaller than in R

I want to subset an interval on the lower end as well as on the upper end along a range of numbers in a variable. My question is how can I subset or filter based on NOT greater than AND NOT smaller than in R? I looked up the relational operators ?'>' and there is nothing like !> or !>=. Then I found that it is possible with !between() from the dplyr package but I cannot figure out how to negate this in base R. How would you do that? Something like !(x[x >= left & x <= right]) doesn't work...

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(ggplot2)
#> Loading required package: ggplot2
x <- seq(-5, 5, length.out = 1000)
d <- tibble(X = x, Y = x^3)

### THIS WORKS
(d %>% 
  filter(!between(X, -2, 2)) -> d_sub)
#> # A tibble: 600 x 2
#>        X     Y
#>    <dbl> <dbl>
#>  1 -5    -125 
#>  2 -4.99 -124.
#>  3 -4.98 -124.
#>  4 -4.97 -123.
#>  5 -4.96 -122.
#>  6 -4.95 -121.
#>  7 -4.94 -121.
#>  8 -4.93 -120.
#>  9 -4.92 -119.
#> 10 -4.91 -118.
#> # ... with 590 more rows

### PLOT TO CONFIRM
(d_sub%>% 
  ggplot(aes(X,Y)) + geom_point())

Created on 2020-01-30 by the reprex package (v0.3.0)

Upvotes: 0

Views: 4536

Answers (2)

akrun
akrun

Reputation: 887531

In base R, we can use subset and specify only the unquoted column names

d_sub2 <- subset(d, !(X >=-2 & X <2))
identical(d_sub, d_sub2)
#[1] TRUE

Or with [

d_sub3 <- d[!(d$X >= -2 & d$X < 2),]

Upvotes: 2

dww
dww

Reputation: 31452

no need to negate anything. You can do (x <= -2) | (x >= 2)

E.g.

X= -10:10

X[(X <= -2) | (X >= 2)]
#[1] -10  -9  -8  -7  -6  -5  -4  -3  -2   2   3   4   5   6   7   8   9  10

Which is the same as

X[!(X >-2 & X <2)]
#[1] -10  -9  -8  -7  -6  -5  -4  -3  -2   2   3   4   5   6   7   8   9  10

Upvotes: 2

Related Questions