Mark
Mark

Reputation: 1769

How define a multivariate function in R

I have the following function:

enter image description here

I would like to write a function in R for f. It should take as arguments x, c1, ..., cn and n.

Upvotes: 4

Views: 477

Answers (1)

akrun
akrun

Reputation: 887531

If the argument lengths are variable, use 3 dots (...)

f1 <- function(x, ...) {
              2 * x + sum((x - c(...))^2)
}

-testing

f1(5, 10, 5, 2, 3)
[1] 48

Upvotes: 7

Related Questions