Evaluates an array of characters or expressions.

evaluate(f, var, params = list(), vectorize = TRUE)

Arguments

f

array of characters or expressions to be evaluated.

var

named vector or data.frame in which f is to be evaluated.

params

list of additional parameters passed to f.

vectorize

logical. Use vectorization? If TRUE, it can significantly boost performance but f needs to handle the vector of inputs appropriately.

Value

Evaluated object. When var is a named vector, the return is an array with the same dimensions of f. When var is a data.frame, the return is a matrix with columns corresponding to the entries of f and rows corresponding to the rows of var.

References

Guidotti E (2022). "calculus: High-Dimensional Numerical and Symbolic Calculus in R." Journal of Statistical Software, 104(5), 1-37. doi:10.18637/jss.v104.i05

See also

Other utilities: c2e(), e2c(), wrap()

Examples

### single evaluation
f <- array(letters[1:4], dim = c(2,2))
var <- c(a = 1, b = 2, c = 3, d = 4)
evaluate(f, var)
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4

### multiple evaluation
f <- array(letters[1:4], dim = c(2,2))
var <- data.frame(a = 1:3, b = 2:4, c = 3:5, d = 4:6)
evaluate(f, var)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    2    3    4    5
#> [3,]    3    4    5    6

### multiple evaluation with additional parameters
f <- "a*sum(x)"
var <- data.frame(a = 1:3)
params <- list(x = 1:3)
evaluate(f, var, params)
#>      [,1]
#> [1,]    6
#> [2,]   12
#> [3,]   18

### multiple evaluation of non-vectorized expressions
f <- "a*myf(x)"
myf <- function(x) if(x>0) 1 else -1
var <- data.frame(a = 1:3, x = -1:1)
evaluate(f, var, params = list(myf = myf), vectorize = FALSE)
#>      [,1]
#> [1,]   -1
#> [2,]   -2
#> [3,]    3