[R] Pass vector as multiple parameters (as in python f(*x))

Bert Gunter gunter.berton at gene.com
Sat Jan 26 01:12:05 CET 2013


Well, of course the answer is yes (it always is!). I'm just not sure
what the question is.

However, I believe you want something like

do.call(the_function, parameter_list).

?do.call  ## for details.

Note that if v is really a (named) vector, it can be converted to a
list via as.list().

Cheers,
Bert


On Fri, Jan 25, 2013 at 3:50 PM, Carlos Pita <carlosjosepita at gmail.com> wrote:
> Hi,
>
> I want to know if it's possible to pass a vector v=c(x,y,...) to a
> function f(x,y,...) so that each vector element corresponds to a
> formal argument of the function. For python programmers: f(*v).
>
> Specifically, what I'm trying to achieve is: given a list of
> coordinates l=list(c(x1,y1,z1), c(x2,y2,z2),...) I would like to
> obtain the corresponding elements in some array A (3-dim in this
> case). That is: A[x1,y1,z1], A[x2,y2,z2],....
>
> One way would be to transform l=list(c(x1,y1,z1), c(x2,y2,z2),...) to
> l2=list(c(x1,x2,...),c(y1,y2,...),c(z1,z2,...)) and then (if this is
> possible at all) execute the equivalent to A[*l2].
>
> Another way would be to lapply function(xyz) { A[*xyz] } to each
> coordinate vector in l. In any case I need the f(*v) equivalent.
>
> Please take into account that, despite the 3-dim example, I need to
> implement the above to accept n-dim vectors for arbitrary n, so
> something like x<-xyz[1], y<-xyz[2], z<-xyz[3] wouldn't fit the bill.
>
> Any other suggested solution would be appreciated.
>
> Best regards
> --
> Carlos
>
> ______________________________________________
> R-help at 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list