I have the following filtering problem using the example data below:
For every negative x, remove that row and the nearest row above it with matching y value.
d <- structure(list(
x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1),
y = c(84, 28, 0, 112, 28, 112, 112, 28, 28, 112, 28, 28,
112, 28, 112, 112, 28, 28, 112, 112, 28, 28, 112, 112, 28, 112,
28, 28, 112, 28, 112, 28, 112, 112, 28)
), class = "data.frame", row.names = c(NA, -35L))
d$exclude <- FALSE
d$exclude[c(10, 13, 32:35)] <- TRUE
print(d)
#> x y exclude
#> 1 1 84 FALSE
#> 2 1 28 FALSE
#> 3 1 0 FALSE
#> 4 1 112 FALSE
#> 5 1 28 FALSE
#> 6 1 112 FALSE
#> 7 1 112 FALSE
#> 8 1 28 FALSE
#> 9 1 28 FALSE
#> 10 1 112 TRUE
#> 11 1 28 FALSE
#> 12 1 28 FALSE
#> 13 -1 112 TRUE
#> 14 1 28 FALSE
#> 15 1 112 FALSE
#> 16 1 112 FALSE
#> 17 1 28 FALSE
#> 18 1 28 FALSE
#> 19 1 112 FALSE
#> 20 1 112 FALSE
#> 21 1 28 FALSE
#> 22 1 28 FALSE
#> 23 1 112 FALSE
#> 24 1 112 FALSE
#> 25 1 28 FALSE
#> 26 1 112 FALSE
#> 27 1 28 FALSE
#> 28 1 28 FALSE
#> 29 1 112 FALSE
#> 30 1 28 FALSE
#> 31 1 112 FALSE
#> 32 1 28 TRUE
#> 33 1 112 TRUE
#> 34 -1 112 TRUE
#> 35 -1 28 TRUE
Created on 2020-02-14 by the reprex package (v0.3.0)
In example data d, that corresponds to removing rows 10, 13, 32, 33, 34, 35. My question is, is there a way to programatically create the exclude column? I've been looking around and trying to use combinations of helper functions such as Position(), which.min(), findInterval() to no avail. Any suggestions would be greatly appreciated!