0

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?

Cœur
  • 34,719
  • 24
  • 185
  • 251
163
  • 195
  • 12
  • Why don't you use `lapply` since it works? I think you have misunderstood why/how to use `do.call` - it is not supposed to do the same thing as `lapply`... Can you provide `fun` and the desired output? – konvas Mar 24 '15 at 14:03
  • @konvas Thanks, I have edit the question, showing why I don't want to use lapply.... for do.call, is it suppose to apply function with different parameters? – 163 Mar 24 '15 at 14:35
  • OK, so you should keep using `lapply` but then you can simplify your result, depending on the format you want it to have. For example, you can covert this list `x – konvas Mar 24 '15 at 14:46
  • By the way, vector subscripts do work with lists, but with single square bracket. Try for example `list(1, 2, 3, 4)[2:3]` – konvas Mar 24 '15 at 14:47
  • @konvas Thanks, the problem is the result is like this. a – 163 Mar 24 '15 at 14:52
  • I see. In `unlist` there is an argument `recursive` which you can try setting to `FALSE`. In any case I mentioned `unlist` just to give you an example, as I said it totally depends on what list you have and what you want to convert it to. If you want to post an exact `dput` of your list and the desired outcome, I can try to help. The point is that `do.call` is completely irrelevant here because it is not designed to do what you want – konvas Mar 24 '15 at 14:54

1 Answers1

3

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))
Joshua Ulrich
  • 168,168
  • 29
  • 327
  • 408
  • those don't work for me....for first case, it says cannot coerce a list to double, which I think that, maybe they need a double value there....second, no error, but the result is wrong... – 163 Mar 24 '15 at 13:51
  • 1
    Then you need to provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Joshua Ulrich Mar 24 '15 at 14:55