An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110125/8f473455/attachment.pl>
function application
4 messages · Roy Shimizu, Erik Iverson, Phil Spector
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
?do.call Please provide a reproducible example if the help file is not sufficient. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu
On Tue, 25 Jan 2011, Roy Shimizu wrote:
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). Thanks! Roy 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. [[alternative HTML version deleted]]
______________________________________________ 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.
On Tue, Jan 25, 2011 at 5:12 PM, Phil Spector <spector at stat.berkeley.edu> wrote:
?do.call
Erik, Phil, thanks! Roy