[R] problem with do.call

Ernesto Jardim ernesto at ipimar.pt
Wed Apr 10 14:00:05 CEST 2002


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 ?



ps: Follows an example (off course the example doesn't make much sense
but describes the problem).

> fun

        vec <- rnorm(25)
        res <- a*vec^b

> fun(2,3)
 [1]  7.006278e+00  3.515010e-01  7.989718e+00 -3.377766e-02
 [6] -2.920680e-01  1.174834e+00 -1.088638e-03  6.448725e+00 
[11] -4.313672e-04 -9.171867e-03 -6.793569e+00 -2.480562e+01
[16] -1.259896e-01 -7.504192e-02  6.647855e-02  5.609645e-01 
[21]  1.802123e+00  7.650033e-03 -3.534951e+00 -2.028473e-03
> do.call("fun",list(a=c(1:6),b=rnorm(6)))
 [1]  1.4766338        NaN  3.0214852  3.8132530  0.2753699        NaN
 [7]        NaN        NaN  2.9998547        NaN        NaN  6.3050385
[13]  0.5970596  0.8722498  2.9931344  4.0664852        NaN        NaN
[19]  2.8121803        NaN  2.9989127        NaN        NaN        NaN
[25] 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 

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

More information about the R-help mailing list