R How to use which() with floating point values?

后端 未结 5 848
走了就别回头了
走了就别回头了 2020-12-11 20:11

I have run into the same problem as described at R which () function returns integer(0)

price = seq(4,7, by=0.0025)
allPrices = as.data.frame(price)
lookupP         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 20:53

    A manual approach to this involves specifying the tolerance for the comparison and doing:

    # tol = 1e-7: comparison will be TRUE if numbers are equal up to 
    #   7 decimal places
    tol = 1e-7
    which(abs(allPrices$price - lookupPrice) < tol)
    

提交回复
热议问题