[R] How to convert List object to function arguments?
Shiazy
shiazy at gmail.com
Sat Mar 3 17:11:06 CET 2007
With a little change it's seem to work:
x <- c(0, 1855, 72, 4830, 493, 424, 193, 2489, 156262, 4557, 958)
params <- list(shape2=5, shape1=2)
do.call( "ks.test", c(list(x), cdfname( "beta" ), params ))
In fact if I do
call("ks.test", list(x=x, cdfname=cdfname, ...))
I get:
ks.test(list(x = c(0, 1855, 72, 4830, 493, 424, 193, 2489, 156262, 4557,
958), y = "pbeta", list(shape2 = 5, shape1 = 2)))
and the error:
Error in y(sort(x), ...) : argument "shape2" is missing, with no default
Instead with the new form:
call( "ks.test", c(list(x), mg_dists_cdfName( "beta" ), parms ))
I get:
ks.test(list(c(0, 1855, 72, 4830, 493, 424, 193, 2489, 156262, 4557,
958), "pbeta", shape2 = 5, shape1 = 2))
and no error.
Thank you so much!!!
-- Marco
hadley wickham wrote:
> On 3/3/07, Shiazy <shiazy at gmail.com> wrote:
>> "do.call" createa a function call and evaluates it. I think this isn't
>> for me. Same thing for "call"; it wants the function name as first
>> argument.
>>
>> However, I have to pass to "ks.test" the CDF function name and,
>> separately, the related arguments.
>>
>> ks.test( x, cdfname, ... );
>>
>> The problem is "..." comes as a list object and I didn't find a way to
>> "coerce" this object to a arguments list, preserving names a values
>> (note each list item can be a complex object such as a vector, matrix,
>> ... so I cannot use unlist o as.array)
>
> I think
> do.call("ks.test", list(x=x, cdfname=cdfname, ...))
> will do what you want.
>
> Hadley
>
> PS. You don't need ; at the end of every line.
>
More information about the R-help
mailing list