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

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

    I just had the exact same problem.

    I initially fixed it by converting both sets of data from numeric to characters with as.character() before calling which().

    However, I wanted to figure out exactly why it wasn't working with the numeric data and did some further troubleshooting.

    It appears that the problem is with the way R generates decimal sequences with seq(). Using the round() function works - as suggested by Tim Biegeleisen - but I think you only need to apply it to the numbers generated by seq(). You can check out my work below - the error is very sporadic, I just tried numbers until I found one that failed: 19.2.

    > data <- 19.2
    > x.seq <- seq(5, 45, 0.2)
    > x.seq[72]
    [1] 19.2
    > 
    > data == 19.2
    [1] TRUE
    > x.seq[72] == 19.2
    [1] FALSE
    > data == x.seq[72]
    [1] FALSE
    > data == round(x.seq[72], digits = 1)
    [1] TRUE
    > round(data, digits = 1) == x.seq[72]
    [1] FALSE
    

提交回复
热议问题