deduped {deduped}R Documentation

Deduplicate a vectorized function to act on unique elements

Description

Converts a vectorized function into one that only performs the computations on unique values in the first argument. The result is then expanded so that it is the same as if the computation was performed on all elements.

Usage

deduped(f)

Arguments

f

Function that accepts a vector or list as its first input.

Value

Deduplicated version of f.

Examples


x <- sample(LETTERS, 10)
x

large_x <- sample(rep(x, 10))
length(large_x)

slow_func <- function(x) {
  for (i in x) {
    Sys.sleep(0.001)
  }
}

system.time({
  y1 <- slow_func(large_x)
})
system.time({
  y2 <- deduped(slow_func)(large_x)
})

all(y1 == y2)

[Package deduped version 0.2.0 Index]