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

后端 未结 5 857
走了就别回头了
走了就别回头了 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:40

    You could also try rounding the prices in your data frame to 4 decimal places:

    which(round(allPrices$price, digits=4) == lookupPrice)
    [1] 425
    

    After rounding to 4 places, the precision of the lookupPrice and your data frame of prices should match.

    Demo

提交回复
热议问题