163
163

Reputation: 205

do.call(), multiple parameters

I have a function with many arguments:

fun(A,B,C,D,E)

Now I want to assign fixed value a,b,c,d to A,B,C,D and assign E a list of 1 : 7

I want to use do.call() as below, but it doesn't work.

a <- do.call(function(x) fun(A = a, B = b, C = c, D = d, E = x), list(1:7))

I turn to lapply, and it works,

a <- lapply(c(1:7), function(x) fun(A = a, B = b, C = c, D = d, E = x))

As Joshua Ulrich's answer, when I try

a `<- do.call(fun, list(A = a, B = b, C = c, D = d, E = list(1:7)))`

it says

(list) object cannot be coerced to type 'double'

So I guess fun needs a double value for E, but do.call() doesn't give the values one by one, but a list.

I don't want to use lapply because it returns a list of list, which, if I want to point at a special list, I have to use [[]], and only single value is allowed in [[]], and I cannot use a vector to point at, e.g. [[a]],with a <- c(1:7).

How to make do.call() work?

Upvotes: 1

Views: 2281

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176718

That should be:

a <- do.call(fun, list(A = a, B = b, C = c, D = d, E = list(1:7)))

And I have a feeling you want E to be a vector, not a list, in which case it should be:

a <- do.call(fun, list(A = a, B = b, C = c, D = d, E = 1:7))

Upvotes: 3

Related Questions