Skip to content
Back to formatted view

Raw Message

Message-ID: <4B483DE9.9080106@ucalgary.ca>
Date: 2010-01-09T08:27:21Z
From: Peter Ehlers
Subject: Arguments of a function
In-Reply-To: <1262974513137-1009883.post@n4.nabble.com>

You could define a list and then just access the
appropriate elements of that list:

my.f <- function(a, b)
{
   x1 = equation 1
   x2 = equation 2
   x3 = equation 3
   L <- list(x1, x2, x3)
   y <- L[[a]] + L[[b]]
}

my.f(1,2)
my.f(2,3)

  -Peter Ehlers

Lisa wrote:
> Dear all,
> 
> I have a question about how to set arguments in my own function. For
> example, I have a function that looks like this:
> 
> my.f <- function(a = x1, b = x2)
> {
>    x1 = equation 1
>    x2 = equation 2
>    x3 = equation 3
>    y = a + b   
> }
> 
> x1, x2, and x3 are temporary variables (intermediate results) calculated
> from other variables within the funciton. I want to use two of these three
> variables to calculate y, and write R script as below:
> 
> my.f(a = x1, b = x2)
>  
> or 
> 
> my.f(a = x2, b = x3)
> 
> The error information shows that: ?objects 'x1', 'x2', or 'x3' not found?. 
> 
> Can anybody help me solve this problem? Thanks in advance.
> 
> Lisa
> 

-- 
Peter Ehlers
University of Calgary
403.202.3921