0

I create a numeric sequence from 0 to 20 by 0.1 units but which() cannot find 15.1 even though it is in the data. But if I join to sequences which() finds it.

a=NULL
a=as.numeric(c(seq(from=0,to=20,by=0.1)))
dat=as.data.frame(a)

which(dat$a==19.8)
which(dat$a==15.1) ## does not work!!! why?
which(dat$a==15.2) ## does not work!!! why?
which(dat$a==15.3) 

## but if I start from 15 it works
a=NULL
a=as.numeric(c(seq(from=15,to=20,by=0.1)))
dat=as.data.frame(a)

which(dat$a==19.8)
which(dat$a==15.1)

## or if I join to sequences it works
a=as.numeric(c(seq(from=0,to=14.9,by=0.1)))
b=as.numeric(c(seq(from=15,to=20,by=0.1)))
c=c(a,b)
dat=as.data.frame(c)
which(dat$c==19.8)
which(dat$c==15.1)
Nohemi
  • 1
  • Hello @Nohemi, Welcome to SO! To make the behavior of the `which()` function constant, you just need to explictly indicate that the decimal number is rounded to 1 by modifying your code as follows `dat=round(as.data.frame(a),1)` and it should work. Cheers. – lovalery Nov 06 '21 at 15:09

0 Answers0