s-news
[Top] [All Lists]

Re: Using apply for functions with more than one argument

To: Luis Apiolaza <luis.apiolaza@forestrytas.com.au>
Subject: Re: Using apply for functions with more than one argument
From: Sarah Goslee <sarah.goslee@gmail.com>
Date: Tue, 7 Jun 2005 08:39:26 -0400
Cc: s-news@lists.biostat.wustl.edu
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=KKTrnOfn2i2x1WhmZkAU6NO8/1zDgSvVLGCQZJUVRCxRyBC7QkamoA7qpOlziIIbMFsOdKAz1WgzwgoHFNmicG6DwfJYzVjWeukdYdq0qQ4C/8djZO+r20iB1XVxLurJLx9X7ZfQi8EP70p2ot2DP1iCQIxQwhUkOcziidAUug4=
In-reply-to: <s2a5b886.096@ft-gateway.forestrytas.com.au>
References: <s2a5b886.096@ft-gateway.forestrytas.com.au>
Reply-to: Sarah Goslee <sarah.goslee@gmail.com>
On 6/7/05, Luis Apiolaza <luis.apiolaza@forestrytas.com.au> wrote:

> 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.

Did you look at the help for apply? 

It gives an example for what you are trying to do.
     x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
     ##- function with extra args:
     cave <- function(x, c1,c2) c(mean(x[c1]),mean(x[c2]))
     apply(x,1, cave,  c1="x1", c2=c("x1","x2"))

You do need to provide an argument x to your function even if you
don't use it, because of the way apply functions. I think.

Sarah
-- 
Sarah Goslee 
http://www.stringpage.com

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