s-news
[Top] [All Lists]

Re: Query: Beginer questions on writing functions

To: "Arjun Bhandari" <arb.eu@adia.co.ae>
Subject: Re: Query: Beginer questions on writing functions
From: John Fox <jfox@mcmaster.ca>
Date: Thu, 22 Aug 2002 08:26:42 -0400
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <OFDA5988D4.E683C9F7-ON44256C1D.002D61C4@adiaweb.adia.co.ae >
Dear Arjun,

At 12:57 PM 8/22/2002 +0400, Arjun Bhandari wrote:

1. How does one check if the correct number of arguments have been input.
     -  I tried to use the following set of statements
        value<-match.call(expand = F)
        if((value$y == NULL) || (value$x == NULL)) stop("Incorrect Number
of arguments to the function")
     - Error: No data to interpret as logical value: e1 || e2
                - I do not quite understand where I am going wrong.

If I follow correctly what you want to do, you can use missing to check whether an argument has been specified; for example,

    > fun <- function(x, y){
    +     if (missing(x) || missing(y)) stop('missing argument')
    +     x + y
    +     }

    > fun(1, 2)
    [1] 3

    > fun(1)
    Problem in fun(1): missing argument
    Use traceback() to see the call stack

On the other hand, if you want your function to take an indefinite number of arguments, you can use the dummy argument ... and check length(c(...)).

2.  Matrix division: I want to get the syntax for solving equations AX=B
and that of XA=B. I get this from matlab but could not find a similar
function in splus. Would anyone know how to get around this.

Again, if I understand what you want, you can use the solve function to solve systems of linear equations; see ?solve.

I hope that this helps,
 John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox@mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------


<Prev in Thread] Current Thread [Next in Thread>