Git Product home page Git Product logo

nanoival's Introduction

Interval type with nanosecond precision for R

Motivation

Thanks to the package nanotime R now has vector of time points with nanosecond precision. nanoival is an interval type that allows complex subsetting into such a vector of time, for example to keep only specific portions of a time point vector.

The goal - not yet implemented - is to use nanoival to subset time-series data.table objects.

Demo

Examples

Creating a nanoival, with the start time included ('+') and the end time excluded ('-')

as.nanoival("+2012-03-01T21:21:00.000000001+00:00->2015-01-01T21:22:00.000000999+04:00-")

a nanoival can also be created with a pair of nanotime objects, a start and an end, and optionally two logicals determining if the interval start(end) are open or closed; by default the start is closed and end is open:

start <- nanotime("2012-03-01T21:21:00.000000001+00:00")
end <- nanotime("2013-03-01T21:21:00.000000001+00:00")
nanoival(start, end)

a vector of nanotime can be subsetted by an interval:

fmt <- "%Y-%m-%d %H:%M:%S"
one_second <- 1e9
a <- seq(nanotime("2012-12-12 12:12:12", fmt), length.out=10, by=one_second)
idx <- c(as.nanoival("-2012-12-12 12:12:10 -> 2012-12-12 12:12:14-", fmt),
         as.nanoival("+2012-12-12 12:12:18 -> 2012-12-12 12:12:20+", fmt))
a[idx]

nanoival also has the set operations union, intersect, setdiff. All are defined for two nanoival arguments, and additionally intersect and setdiff can have the first argument as a nanotime and the result is then a nanotime instead of an nanoival.

a <- seq(nanotime("2012-12-12 12:12:12", fmt), length.out=10, by=one_second)
i <- as.nanoival("-2012-12-12 12:12:14 -> 2012-12-12 12:12:18-", fmt)
setdiff(a, i)

i1 <- as.nanoival("+2012-12-12 12:12:14 -> 2012-12-12 12:12:17-", fmt)
i2 <- as.nanoival("+2012-12-12 12:12:16 -> 2012-12-12 12:12:18-", fmt)
union(i1, i2)

Finally, intersect.idx which gives back the indices of the intersection is defined.

a <- seq(nanotime("2012-12-12 12:12:12"), length.out=10, by=one_second)
idx <- as.nanoival("+2012-12-12 12:12:14 -> 2012-12-12 12:12:19+")
intersect.idx(a, idx)
## which gives back:
## $x
## [1] 3 4 5 6 7 8
## 
## $y
## [1] 1 1 1 1 1 1
                                                                             

union.idx and setdiff.idx will be implemented in the future.

Status

The package is in the very early stages and is largely untested.

See the issue tickets for an up to date list of potentially desirable, possibly planned, or at least discussed items.

Installation

remotes::install_github("lsilvest/nanoival")

Author

Leonardo Silvestri

License

GPL (>= 2)

nanoival's People

Contributors

lsilvest avatar

Stargazers

Joshua Ulrich avatar

Watchers

James Cloos avatar Jan Gorecki avatar

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.