Take the following simple function:
fun <- function(a, b, c, d, e) {
stopifnot("Input you provide must be equal length." = length(a) == length(b) && length(b) == length(c) && length(c) == length(d) && length(d) == length(e))
result <- (a + b / c + d) / sqrt(e)
result2 <- a/result
return(data.frame(result = result, result2 = result2, a = a, b = b, c = c, d = d, e = e))
}
Now, if I want to map over a look-up table of all combinations of input values, I could do the following, e.g., using purrr
functionals:
library(purrr)
df <- expand.grid(a = 1:1000, b = c(1, 2, 3, 4, 5), c = 7, d = 3, e = 5)
out <- pmap_df(d, fun)
However, even for the relatively simple case of one larger and a smaller vector (in my application case, this would be the most common case though), this is pretty slow.
Unit: seconds
min lq mean median uq max neval
2.235245 2.235245 2.235245 2.235245 2.235245 2.235245 1
How to speed this up, especially for the simple case sketched above? Of course, as df
gets larger and larger, things will slow down.