# [R] problem with do.call

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Apr 10 14:11:03 CEST 2002

```Ernesto Jardim <ernesto at ipimar.pt> writes:

> Hi
>
> I'm writing a function that uses four parameters (scalars) and I need to
> run it in an iterative process (the parameters vary to find the minimum
>
> I don't want to use loops and so tried the do.call function. However it
> didn't work. My understanding is that the do.call simple runs the
> function replacing the arguments (scalars by vectors), instead of runing
> the function for each set of scalars in the list, what I need.
>
> Can you please tell me if there is another way of doing it whithout
> using the for loop ?

That's not what do.call does. It is for situations where the argument
list of a single call needs to be constructed from simpler components.
Your example is equivalent to fun(a=c(1:6), b=rnorm(6))

The loop over multiple parallel vectors is only doable via something
like lapply(1:6, function(i)fun(a[i],b[i]))

However, I recently played with this and got as far as this:

napply <-
function(..., FUN) {
FUN <- match.fun(FUN)
x <- list(...)
lens <- sapply(x,length)
len <- max(lens)
if (any(lens != len))
x <- lapply(x, rep, length=len)
tuples <- lapply(seq(length=len), function(i)lapply(x,"[", i))
sapply(tuples, function(t)eval(as.call(c(FUN,t))))
}

>  napply(a=c(1:6),b=rnorm(6), FUN=fun)
[,1]     [,2]     [,3]       [,4]     [,5]        [,6]
[1,] 1.0259135      NaN 3.003882        NaN      NaN   20.299212
[2,]       NaN 1.977696 3.026111        NaN 3.951746   19.107481
[3,] 1.1840499 2.024837      NaN   8.289768      NaN    7.479917
[4,] 0.9756922 2.003576      NaN        NaN 4.236000         NaN
[5,] 1.0010550 2.006045      NaN        NaN      NaN 1302.330425
[6,]       NaN      NaN      NaN   2.472650      NaN         NaN
[7,]       NaN 2.094956      NaN        NaN      NaN    3.685879
[8,] 0.8646628      NaN 2.993435        NaN 3.369501         NaN
[9,]       NaN 2.044915 3.006433   6.426090 6.123980   19.235790
[10,] 1.6051736      NaN 3.011986        NaN 3.638641         NaN
....

> > fun
> function(a,b){
>
>         vec <- rnorm(25)
>         res <- a*vec^b
>         res
>
> }
> > fun(2,3)
>    7.006278e+00  3.515010e-01  7.989718e+00 -3.377766e-02
> -1.879471e-02
>   -2.920680e-01  1.174834e+00 -1.088638e-03  6.448725e+00
> 2.591805e+00
>  -4.313672e-04 -9.171867e-03 -6.793569e+00 -2.480562e+01
> -1.514828e+01
>  -1.259896e-01 -7.504192e-02  6.647855e-02  5.609645e-01
> 1.093114e-01
>   1.802123e+00  7.650033e-03 -3.534951e+00 -2.028473e-03
> -2.837360e+01
> > do.call("fun",list(a=c(1:6),b=rnorm(6)))
>    1.4766338        NaN  3.0214852  3.8132530  0.2753699        NaN
>          NaN        NaN  2.9998547        NaN        NaN  6.3050385
>   0.5970596  0.8722498  2.9931344  4.0664852        NaN        NaN
>   2.8121803        NaN  2.9989127        NaN        NaN        NaN
>  14.4631627
> Warning messages:
> 1: longer object length
>         is not a multiple of shorter object length in: vec^b
> 2: longer object length
>         is not a multiple of shorter object length in: a * vec^b
> >

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```