0

I am having some problems with dplyr::filter. I have a simple table with two numeric columns. When I attempt to filter, it lets me filter on some of the values but not others. This seems like it is a basic use of dplyr::filter (and therefore, my error should be obvious), but I've been unable to figure out where I am going wrong. R 3.4.1. dplyr 0.7.2. Windows machine. Not sure what other info would be helpful (if any).

Example:

cad.df <- expand.grid(
  ca.prop = seq(0.1, 1, 0.1),
  fl_m = seq(0.070, 0.130, 0.020)
)

library(dplyr)

cad.df %>%
  filter(fl_m == 0.070)  # works

   ca.prop fl_m
1      0.1 0.07
2      0.2 0.07
3      0.3 0.07
4      0.4 0.07
5      0.5 0.07
6      0.6 0.07
7      0.7 0.07
8      0.8 0.07
9      0.9 0.07
10     1.0 0.07

cad.df %>%
  filter(fl_m == 0.090)  # does not work

[1] ca.prop fl_m   
<0 rows> (or 0-length row.names)

cad.df %>%
  filter(fl_m == 0.110)  # does not work

[1] ca.prop fl_m   
    <0 rows> (or 0-length row.names)

cad.df %>%
  filter(fl_m == 0.130)  # works

   ca.prop fl_m
1      0.1 0.13
2      0.2 0.13
3      0.3 0.13
4      0.4 0.13
5      0.5 0.13
6      0.6 0.13
7      0.7 0.13
8      0.8 0.13
9      0.9 0.13
10     1.0 0.13

unique(cad.df$fl_m)

[1] 0.07 0.09 0.11 0.13

Anyone else experience anything like this? Or see what I am doing wrong?

Thanks for any help.

Cheers

ew_co
  • 1
  • It's a bad idea to attempt equality comparisons with floating point numbers (see the duplicate question about why). Instead test that the difference is smaller than some amount: `filter(abs(fl_m-0.110)<.0001)` – MrFlick Jul 25 '17 at 23:18
  • Ahh, gotcha. Thanks! That did the trick. It also looks like dplyr's near() would work too. – ew_co Jul 26 '17 at 02:30

0 Answers0