Wolfgang Waser
2008-Sep-01 17:41 UTC
[R] how to pass additional parameters to a function called in tapply?
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
jim holtman
2008-Sep-01 19:28 UTC
[R] how to pass additional parameters to a function called in tapply?
try: tapply(data,factor-vectors,avg.f, part) On Mon, Sep 1, 2008 at 1:41 PM, Wolfgang Waser <wolfgang.waser at utu.fi> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?