s-news
[Top] [All Lists]

Re: Using apply for functions with more than one argument

To: "'Luis Apiolaza'" <luis.apiolaza@forestrytas.com.au>, <s-news@lists.biostat.wustl.edu>
Subject: Re: Using apply for functions with more than one argument
From: <Rich@Mango-Solutions.com>
Date: Tue, 7 Jun 2005 15:28:14 +0100
In-reply-to: <s2a5b886.096@ft-gateway.forestrytas.com.au>
Thread-index: AcVrHxwgTJWs0RbHTQi+N3gCfL0/fgATSgdA
If you want to use apply in the way you describe, something like this should
work ...

> myMat <- matrix(rpois(20, 3), ncol=2)    # Create some data
> myFun <- function(x, y) x + y                # Create a function taking 2
numbers
> apply(myMat, 1, function(x) do.call("myFun", as.list(x))) # Make the call

Cheers,
Rich.

S & R Training and Consulting
mangosolutions
Tel   +44 118 902 6617
Fax  +44 118 902 6401

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Luis Apiolaza
Sent: 07 June 2005 06:09
To: s-news@lists.biostat.wustl.edu
Subject: [S] Using apply for functions with more than one argument

Hi,

Let's say that I have an array with 100,000 rows and two columns (total
height and stem diameter for trees), and that I have defined a
stem.volume (non-linear, by the way) function that requires total height
and stem diameter as arguments.

Is it possible to use apply in some way to calculate stem.volume? 

All examples that I have seen for 'apply' use functions with only a
single argument. Is there a more general way of using apply for more
complex functions? I just want to avoid using an explicit loop.

Cheers,


Luis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr Luis A. Apiolaza
Principal Forest Biometrician
Forestry Tasmania
GPO Box 207
Hobart Tas 7001
Australia

email:    Luis.Apiolaza@forestrytas.com.au
phone: +61-3-6233 8127
web:     http://www.forestrytas.com.au
             http://uncronopio.org
"All I want is a warm bed, a kind word,
 and unlimited power" --Ashleigh Brilliant
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------
This transmission is intended solely for the person or 
organisation to whom it is addressed.
It is confidential and may contain legally privileged 
information. If you have received this transmission in 
error,you may not use, copy or distribute it.
Please advise us by return e-mail or by phoning 61 3 62338203 
and immediately delete the transmission in its entirety.
We will meet your reasonable expenses of notifying us.
Despite our use of anti-virus software, Forestry Tasmania 
cannot guarantee that this transmission is virus-free. 
-----------------------------------------------------
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news




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