Re: [R] Evaluating a multivariable function XXXX

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Sat, 07 May 2011 09:05:02 -0400

On 11-05-07 4:06 AM, Dan Abner wrote:
>>
>> Hello everyone,
>>
>> I have the following R code for a multivariable function:
>>
>>
>>> fn2<-function(x,y,z){(y+2*z)/(5*y-x*z)}
>>>
>>> fn2(-5,-2,3)
>> [1] 0.8
>>
>>
>>
>> No problems.
>>
>> ===
>>
>> If, however, I call the function using a vector substitution for the
>> arguments, R sees this as 3 separate calls to the function while supplying
>> only the first argument:
>>
>>> in2<-c(-5,-2,3)
>>> in2
>> [1] -5 -2 3
>>>
>>> fn2(in2)
>> Error in fn2(in2) : argument "y" is missing, with no default
>>
>> ===
>>
>> How should I call the function using the vector substitution method so that
>> R sees that this is a single call to the function that supplies all 3 of the
>> arguments?

The simplest way is to write a new wrapper function:

fn3 <- function(xyz) fn2(xyz[1], xyz[2], xyz[3])

You can also do it with do.call:

do.call(fn2, as.list(in2))

Duncan Murdoch



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. Received on Sat 07 May 2011 - 13:09:26 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 07 May 2011 - 14:10:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive