Marc,
There are two problems with the function definition
> myfunction<-function(x,y)
> {
> apply(x, 2, function(x,y) { tapply (x, y, mean)}
> }
The first problem here is conceptual. The second, internal function
definition should be just function(x), not (x,y). Since you are applying
a function to the columns of a matrix (or perhaps to cuts of an array
defined by its second dimension), that function is a function of just
one argument (each column vector, in turn). Other structures, such as y,
can be passed to it for use, but they are not arguments of the internal
function. Thus, you would want:
myfunction<-function(x,y)
{
apply(x, 2, function(w) { tapply (w, y, mean)}
}
Note, by the way, that I have also changed x to w. That isn't essential,
but it is confusing to use the same symbol for a bound variable and
a free variable in the expression on that line, and I always avoid
such confusing usage when I can. It is important to recognize that
w as a bound variable, quite different from x; the latter is bound only
in the top-level definition but is free in the substatement. In the
same way, I never write an integral from 0 to t of f(t)dt, since the
t in the integral limit is free and that in the integrand is bound;
rather, integral from 0 to t of f(s)ds is much clearer.
However, the function still won't work, although you will get a different
error message. The second problem is that the argument y to the top-level
function is not assigned in a frame that is examined by the second function.
One has to make y available by assigning it explicitly before the apply()
step. Thus, the correct version should read:
myfunction<-function(x,y)
{
assign("y", y, frame=1)
apply(x, 2, function(w) { tapply (w, y, mean)}
}
That does work.
Dave Krantz
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|