MyNixOS website logo
Description

Simple Wrapper for Computationally Expensive Functions.

This is a one-function package that will pass only unique values to a computationally-expensive function that returns an output of the same length as the input. In importing and working with tidy data, it is common to have index columns, often including time stamps that are far from unique. Some functions to work with these such as text conversion to other variable types (e.g. as.POSIXct()), various grep()-based functions, and often the cut() function are relatively slow when working with tens of millions of rows or more.

calcUnique Package

This is a one-function package so that you can pass only unique values to a computationally expensive function that returns an output of the same length as the input.

In importing and working with tidy data, it is common to have index columns, often including time stamps that are far from unique. Some funcitons to work with these such as text conversion, various grep()-based functions, and often the cut() function are relatively slow when working with tens of millions of rows or more.

This wrapper function pares down the input vector to process to only unique values using hte unique() function, and in my experience the unique() and match() functions upon which calcUnique() is based are so fast that only a small amount of repition is necessary to make calcUnique the faster option.

calcUnique() is a wrapper for any function that takes in a vector or list and returns a vector or list the same length. The inputs and outputs are the same as they would be otherwise … it just happens faster.

Examples:

#Create a sample of some date text with repeats
ts_sample <-
  sample(as.character(seq(
    from = as.POSIXct('2020-03-01'),
    to = as.POSIXct('2020-03-05'),
    by = 'day'
  )),
  size = 10, replace = TRUE)

ts_sample
##  [1] "2020-03-01" "2020-03-02" "2020-03-02" "2020-03-02" "2020-03-04"
##  [6] "2020-03-05" "2020-03-05" "2020-03-01" "2020-03-01" "2020-03-01"
#Now convert the time text back to POSIXct timestamps:
as.POSIXct(ts_sample)
##  [1] "2020-03-01 MST" "2020-03-02 MST" "2020-03-02 MST" "2020-03-02 MST"
##  [5] "2020-03-04 MST" "2020-03-05 MST" "2020-03-05 MST" "2020-03-01 MST"
##  [9] "2020-03-01 MST" "2020-03-01 MST"
#Do the same with the calcUnique function:
calcUnique(ts_sample, as.POSIXct)
##  [1] "2020-03-01 MST" "2020-03-02 MST" "2020-03-02 MST" "2020-03-02 MST"
##  [5] "2020-03-04 MST" "2020-03-05 MST" "2020-03-05 MST" "2020-03-01 MST"
##  [9] "2020-03-01 MST" "2020-03-01 MST"
#Just to show that the output is the same with and without calcUnique:
all.equal(as.POSIXct(ts_sample), calcUnique(ts_sample, as.POSIXct))
## [1] TRUE
#An example for when the function doesn't take the vector as the first argument:
gsub("03", "$3", ts_sample)
##  [1] "2020-$3-01" "2020-$3-02" "2020-$3-02" "2020-$3-02" "2020-$3-04"
##  [6] "2020-$3-05" "2020-$3-05" "2020-$3-01" "2020-$3-01" "2020-$3-01"
calcUnique(ts_sample, function(i) gsub("03", "$3", i))
##  [1] "2020-$3-01" "2020-$3-02" "2020-$3-02" "2020-$3-02" "2020-$3-04"
##  [6] "2020-$3-05" "2020-$3-05" "2020-$3-01" "2020-$3-01" "2020-$3-01"
Metadata

Version

0.1.2

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows