[R] how to pass additional parameters to a function called in tapply?
Wolfgang Waser
wolfgang.waser at utu.fi
Mon Sep 1 19:41:38 CEST 2008
Hi all,
the following problem is still beyond my R-knowledge:
I have one data vector containing the signal from 4 channels that are measured
subsequently and in repeating cycles (with one factor vector for cycle and
one for channel identification).
To extract the mean of each channel during each cycle tapply is the method of
choice. However, I cannot use the whole measuring period for each channel as
marked in the factor vector since the hardware switching between channels
causes some transients in the signal which should not be included when
calculating the mean.
Instead of using "mean" as function within tapply, I used the following set of
functions:
start_end.f <- function(x) { c(length(x)*(1-part),length(x)) }
to define the part of the data to use (e.g. part=0.5 the last 50% of the total
period),
#(part has so far been set universally once for the whole analysis, but see
below)#
avg.f <- function(x) {
range <- start_end.f(x)
mean(x[range[1]:range[2]])
}
and then
tapply(data,factor-vectors,avg.f)
to extract the mean of a subsample of the total measuring period of each
channel.
The problem now is that I have several signals and different 'parts' should be
analysed for each, so functions have to be changed to
start_end.f <- function(x,part) { ... }
avg.f <- function(x,part)
range <- start_end.f(x,part)
mean(x[range[1]:range[2]])
}
However, this causes an error
Error in FUN(X[[1L]], ...) : argument "part" is missing, with no default <-
since tapply seems to be unable to pass more than x to the function called
Is there a way to pass variables to the function called in tapply other than
the dataset extracted by tapply?
Thanks for any help
Wolfgang
More information about the R-help
mailing list