apply {msProcess} | R Documentation |
The apply
function in S-PLUS is S3 generic,
but it is not so in R. For the msProcess package, the apply
function is overloaded to be an S3 generic function, relying on
UseMethod("apply")
to distribute the call. The apply.default
function is defined to be base::apply
, so if the class of
the X
input is not "msSet"
then the standard R definition will
be used.
apply(X, MARGIN, FUN, ..., type="intensity", pre=NULL, covar=NULL)
X |
an msSet object. |
MARGIN |
an integer denoting the dimension over which the
given function is applied. Use MARGIN=1 for rows and
MARGIN=2 for columns. |
FUN |
a function to be applied to the specified array sections, or a character string giving the name of the function. |
... |
any arguments to FUN . They are passed unchanged to
each call of FUN and include their names. |
type |
a character string specifying the name of the array in the msSet
object list to operate over. A typical value is "intensity" or "noise" ,
but the name of any legitimate matrix attached to the primary
msSet object list can be used for type .
Default: "intensity" (the intensity matrix). |
pre |
a function that is applied to the matrix prior to
processing the data. Typical examples would be pre=t (matrix
transpose), pre=log (log of matrix), etc. Default: NULL
(no function is applied a priori). |
covar |
a named list of additional matrices to be parsed in the
same manner as the primary matrix (specified by type ). The
contents of the covar matrices are also sent to the FUN
function as an input argument with the same name as that supplied in
the covar list. As an example, assuming x is an object
of class msSet that contains the matrices x$intensity
and x$z , then the call:
apply(x, MARGIN=1, FUN="foo", type="intensity", covar=list(z=z)) ,
will ultimately result in calls foo(x$intensity[i,], z=x$z[i,]) , where i=1:numRows(x$intensity) .
Note that the matrix
x$z need only have the same number of rows in this case since
MARGIN=1 , but need not necessarily contain the same number
of columns, i.e., restrictions on the dimensions of covar matrices
not specfied by MARGIN are controlled by the FUN function.
Default: NULL (no covariate matrices). |
a matrix containing the result of the FUN
function applied to the matrix
of type type
found in the original msSet object X
.
if (!exists("qcset")) data("qcset", package="msProcess") # find the means of each spectrum # and convert the result to a single-row matrix means <- apply(qcset, MARGIN=2, mean) nc <- NCOL(qcset$intensity) means <- matrix(means, ncol=nc) print(means) # add the means (single-row) matrix to the original # msSet object and verify its existence z <- msSet(qcset, means=means) names(z) is.matrix(z$means) # to illustrate the use of the 'covar' argument in apply, # create a faux function that finds tha maximum absolute # difference between each spectrum and its mean value foo <- function(x,meanvals) max(abs(x-meanvals)) maxdiff <- as.vector(apply(z, MARGIN=2, FUN=foo, covar=list(meanvals=means))) print(maxdiff) # verify the results: should get vector of nc zeros unlist(lapply(seq(along=maxdiff), function(i,z,maxdiff) vecnorm(max(abs(z$intensity[,i]-z$means[,i])) - maxdiff[i]), z=z, maxdiff=maxdiff))