Hello,

I have a function with a long list of parameters (of different types,
numeric and string)

myFunc <-function(p1, p2, p3, p4, p5...etc)
{
   do.something(p1,p2,....)
}

I want to loop over this to provide a different set of parameters to the
list every time.

for (ii in 1:N)
{
   myFunc(p1(ii), p2(ii),....etc)
}

I would like to simplify the notation and use some kind of structure, maybe
a list or dataframe.
However when I try, for example, the following:

params = data.frame(cbind(p1=c(1,2,3), p2=5, p3=c(3,2,1)))
myFunc <-function(params)
{
   attach(params)
   do.something(p1,p2,....)
   detach(params)
}
for (ii in 1:N)
{
   myFunc(params[ii,])
}


do.something fails because p1, p2,... are now factors, and i have to
as.numeric() or as.char() each parameter before applying mathematical
operators (such as simple multiplication).
This defeats the purpose of simplifying the code.

Is there any decent, simple way of doing this?
Can I unfactor a row of a data.frame? or a list?
(BTW, "as.numeric()" won't work since some of the parameters are strings,
same goes for stringsAsFactors in data.frame)

Thanks,
Eran

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to