[R] How to convert a list to a ... argument for a function
Charles C. Berry
cberry at tajo.ucsd.edu
Tue Oct 5 20:40:30 CEST 2010
On Tue, 5 Oct 2010, Richard R. Liu wrote:
> Hi,
>
>
> I have a function f <- function(..., func){ something }, where func is a
> function of the form function(...). Â I would like to pass func all the arguments
> passed to f except the last. Â I know that I can manipulate the variable number
> of arguments passed to f by converting ... to a list, i.e., arglist <-
> list(...). Â But how do I pass func the first n-1 list items of arglist (n <-
> length(arglist)), as n-1 arguments, not as one list of n-1 items?
>
One way:
foo <- function(...,func){
mc <- match.call()
mc[[1]] <-mc$func
mc$func <- NULL
mc[[ length(mc) ]] <- NULL
eval(mc) }
foo(1,2,3,4,func=c)
foo(1,2,4,3,func=sum)
or perhaps you meant like this:
foo <- function(...,func){
mc <- match.call()
mc[[1]] <-mc$func
mc$func <- NULL
eval(mc) }
foo(1,2,3,4,func=c)
foo(1,2,4,3,func=sum)
HTH,
Chuck
>
> Regards,
> Richard
>
>
>
> Richard R. Liu
> richard.liu at pueo-owl.ch
>
> ______________________________________________
> 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list