0

i have an issue while try to filtering a df and I don´t understand why. The problem is just as simple as this:

view(df)
class(df)
.class2(df[[1]])
df
f <- df[df[,1] == 1,]
f

with the following output:

> class(df)
[1] "data.frame"
> .class2(df[,1])
[1] "double"  "numeric"
> df
          cod uncod
1  -0.8333333 0.040
2  -0.8333333 0.040
3  -0.8333333 0.040
4  -0.8333333 0.040
5  -1.0000000 0.038
6  -1.0000000 0.038
7   1.0000000 0.062
8   1.0000000 0.062
9  -0.8333333 0.040
10 -0.8333333 0.040
11 -0.8333333 0.040
> f <- df[df[,1] == 1,]
> f
[1] cod   uncod
<0 Zeilen> (oder row.names mit Länge 0)

while doing this works fine:

f <- df[df[,1] == -1,]
> f
  cod uncod
5  -1 0.038
6  -1 0.038
> 

I really don´t understand what´s happening since it is working with a new data.frame:

df2<-data.frame(cod=c(-0.83,-1,0,1),uncod=c(0.040,0.038,0.05,0.062))
> f2 <- df[df[,1] == 1,]
> f2
  cod uncod
4   1 0.062
> 

What is wrong with my first df? Sorry that I don´t have a representative example but the data.frame is coming out of a bigger code.

Marcel
  • 51
  • 6
  • 4
    Almost certainly it's floating point precision issues. Try `df[abs(df[,1] - 1) < 1e-6, ]`, and see the FAQ [Why are these numbers not equal?](https://stackoverflow.com/q/9508518/903061) for explanation. – Gregor Thomas Mar 30 '23 at 14:20
  • 1
    @r2evans jup I got confused, sorry about that. thanks for the explanation – szmple Mar 30 '23 at 14:36

1 Answers1

0

Thanks to the comments I found the solution. Was an issue with floating point precision. Works fine with dplyr::near

x <- 1 # Filter criteria
f <- df %>% filter(near(df[[1]],x)==TRUE)

not as good as the base function but it works.

Marcel
  • 51
  • 6