s-news
[Top] [All Lists]

how do arguments work?

To: s-news@wubios.wustl.edu
Subject: how do arguments work?
From: Carlisle Thacker <thacker@aoml.noaa.gov>
Date: Thu, 19 Apr 2001 14:07:37 -0400 (EDT)
In-reply-to: <87snj4lveo.fsf@jeeves.blindglobe.net>
How do I pass a second orgument to the applied FUN of by() when by()
is used within another function?  Here is the simplest test case:

> test.fcn <- function(a,b,x){by(a,b,function(data,x)x*data)}
> test.a <- data.frame(u=1:4,v=c(1,1,2,2))
> test.a
  u v 
1 1 1
2 2 1
3 3 2
4 4 2
> test.fcn(test.a,test.a$v,3)
Problem in FUN(x[i,  , drop = F]): argument "x" is missing with no default 
Use traceback() to see the call stack

On the other hand:

> test.fcn <- function(a,b,x){by(a,b,function(data,...)x*data)}
> test.fcn(test.a,test.a$v,3)
b:1
           u         v 
1  -97.03125 -97.03125
2 -194.06250 -97.03125
------------------------------------------------------------
b:2
          u         v 
3 -291.0938 -194.0625
4 -388.1250 -194.0625

These wild numbers seem to have been caused by an unrelated data
object x, which is a vector with first element x[1] = -97.03125, which
enters via ... .


Thanks.  Carlisle

********************************************************
* William Carlisle Thacker                             *
*                                                      *
* Atlantic Oceanographic and Meteorological Laboratory *
* 4301 Rickenbacker Causeway                           *
* Miami, Florida 33149 USA                             *
* Office:  (305) 361-4323                              *
* Fax:     (305) 361-4392                              *
********************************************************


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