Code Monkey home page Code Monkey logo

Comments (2)

DavisVaughan avatar DavisVaughan commented on June 11, 2024 1

This is a typical problem with floating point numbers. Manually typing in 7.0001 and generating the supposedly equivalent value with seq() results in two different numbers under the hood:

options(digits = 22)

seq(0,10, 0.0001)[70002]
#> [1] 7.00010000000000065512

7.0001
#> [1] 7.000099999999999766942

They are close, but not quite the same. There is a nice python article about this:
https://docs.python.org/3/tutorial/floatingpoint.html

In general, filtering or joining on fractional numbers like this is going to be problematic. For filtering, one solution is to use near():

library(dplyr)
longseq <- data.frame(time = seq(0,10, 0.0001))
longseq[70002,]
#> [1] 7.0001
longseq %>% filter(near(time, 7.0001))
#>     time
#> 1 7.0001

For joins, you should try to avoid using floating point numbers if you can, or at least round them to the nearest second first if they are times. If you really need fractional seconds, you could look into nanotime or clock, which have "true" subsecond types that aren't backed by floating point types, so they would work better for that.

Other than that, there isn't much dplyr can do here

from dplyr.

PStaus avatar PStaus commented on June 11, 2024

Thank you Davis Vaughan!
this helped me a lot. And thank you for the very fast reply and pointing to the other two packages. I will have a look on using different classes for joining data frames.

from dplyr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.