[R] do.call with Vectorial Argument

Lorenzo Isella lorenzo.isella at gmail.com
Tue Oct 3 16:46:28 CEST 2006


Dear All,
I am trying to use the do.call command to avoid tedious loops in R
when I need to call repetitively a function.
I am experiencing a problem when the arguments of a function are given
with a vector (I need to express then this way to be able later on to
integrate them numerically with the adapt package).
Consider the small script:

remove(list=ls())
#library(adapt)

# first a list of the parameters

tau<-0.1
beta<-1/tau
St<-tau
D=2e-2
q<-2*beta^2*D
lam1<- -beta/2*(1+sqrt(1-4*St))
lam2<- -beta/2*(1-sqrt(1-4*St))
x0<- 0
vx0<- 1

# fist a function with scalar parameters

sigma_pos_old<-function(t,q,lam1,lam2)
{
q/(lam1-lam2)^2*(
(exp(2*lam1*t)-1)/(2*lam1)-2/(lam1+lam2)*(exp(lam1*t+lam2*t)-1) +
(exp(2*lam2*t)-1)/(2*lam2) )
}

# now the same function where the only argument t is given as a 1x1 vector

sigma_pos<-function(myargs)
{
q/(lam1-lam2)^2*(
(exp(2*lam1*myargs[1])-1)/(2*lam1)-2/(lam1+lam2)*(exp(lam1*myargs[1]+lam2*myargs[1])-1)
+ (exp(2*lam2*myargs[1])-1)/(2*lam2) )
}

# Now I use do.call


newtime<-seq(1,5,len=1001)

mypar<-c(q,lam1,lam2)
sig_xx<-do.call("sigma_pos_old",c(list(t=newtime),mypar))

# Now this line does not work; sig_xx2 is not a vector equal to sig_xx

sig_xx2<-do.call("sigma_pos",c(list(myargs =newtime)))




So I am making some mistake with the do.call command, since I get a
single value rather than a vector.
Can anyone tell me what I am doing wrong? I tried several variations
of the syntax, but none works the way I want.
Kind Regards

Lorenzo



More information about the R-help mailing list