Suppose I have a function, like list, that takes a variable number of
arguments, and I have those arguments in some vector x.  How can execute the
function with the *contents* of x as its arguments?  I.e., I don't want
list(x), but rather list(x[[1]], x[[2]], ..., x[[n]]), but I don't want to
spell out the individual elements of x (either because I want to do this
programmatically, so I cannot code an expression like
list(x[[1]],...,x[[n]]) because the value of n is not know until run time,
or else, simply to avoid the tedium of typing out all the elements of x
individually).

In this particular case, because you want to create a list,

x <- 1:10
as.list(x)

will do.

P.S. In Python, if x is some sequence-like object (e.g. a list or a tuple),
and f is some function, the expression f(*x) causes f to be called with the
*contents* of x as its arguments.  (This is to be distinguished from f(x),
which calls f with x as its sole argument.)  In Mathematica, one can achieve
a similar effect using the Apply function: Apply[f, x].  I'm looking for the
equivalent of this in R.

In general, if you already have a *list* and want to call a function
with the contents of that list as the arguments, then ?do.call is what
you need.

a <- list("example", "of", "do.call")

#compare the two following expressions
paste(a)
do.call(paste, a)


--Erik

______________________________________________
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