df <- data.frame(x = c(6.00001, 6.00000, 5.99999, 5, 2), y = c(1, 2, 3, 4, 5))
x y
1 6.00001 1
2 6.00000 2
3 5.99999 3
4 5.00000 4
5 2.00000 5
I can use df[df$x == 6,] to quickly return the rows that has x == 6,
x y
2 6 2
but what if I want to have a tolerance here? all.equal seems not applicable here:
df[all.equal(df$x, 6, 0.0001), ]
x y
NA NA NA
If I want to find the rows that x are very close to 6, is there a short way to do it? Expected output:
x y
1 6.00001 1
2 6.00000 2
3 5.99999 3
You can use near(), which is a wrapper for abs(x - y) < tol:
library(dplyr)
df %>%
filter(near(x, 6, tol = 1e-04))
x y
1 6.00001 1
2 6.00000 2
3 5.99999 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With