Last data update: 2014.03.03

R: Extract and Aggregate Objects of Class 'mefa'
aggregate.mefaR Documentation

Extract and Aggregate Objects of Class 'mefa'

Description

These methods are useful when extracting or aggregating 'mefa' objects. The count data matrix, the matrices for segments and the linked tables of samples and taxa are extracted or aggregated at the same time.

Usage

## S3 method for class 'mefa'
aggregate(x, by.samp = NULL, by.taxa = NULL, ...)
## S3 method for class 'mefa'
x[i = 1:dim(x)[1], j = 1:dim(x)[2], k = 1:dim(x)[3], drop = FALSE]
xtab(x, ...)
samp(x, ...)
taxa(x, ...)
segm(x, ...)

Arguments

x

an object of class 'mefa'.

by.samp

a vector with length as the number of samples and more than one unique values to use aggregating samples.

by.taxa

a vector with length as the number of taxa and more than one unique values to use aggregating taxa.

i, j, k

index value or a vector of indices for samples, taxa and segments, respectively. Can be numeric or character. Negative numeric values indicate items to be excluded.

drop

logical, if TRUE the unused factor leves in the linked tables for samples and species will be dropped after extraction. If FALSE (default) unused factor levels are left intact.

...

other arguments passed to the generic function.

Details

If a 'mefa' object is aggregated, either or both tables for samples and taxa (depending on if the aggregation was made by rows, columns or both) are set to NULL. Because aggregation is not straightforward for data frames with mixed type of data (e.g. factors, character vectors), and the applied function (mean, sum, etc.) depends on the type of the numeric variables. Consequently, if new data table is needed, that must be defined again by the function mefa. If the original data frame contained only numeric columns, it can be easily aggregated if the function to apply is known. For factors and numeric columns, however, such aggregation is not defined.

The ordering of dimensions follow the ordering of the indices in the extraction call, except for nested subsets, when sequence of nested segments is not allowed to be affected by the extraction (error message is given). The extracting method retains samples and taxa tables as well. If non-nested segments are extracted, the x$xtab matrix will contain the sum of segments, and not the original values of x$xtab. Thus if a resulting 'mefa' object has only one segment, it will be identical to the x$xtab matrix. If nested segments are extracted, x$xtab will be identical to the last segment and not the sum of segments. See the examples for these differences. If the original object had nested segments and the first is excluded, segment names change as a result. If only one segment remains in the result, the nested attribute is turned off.

If the aggregation is done over a vector that is not part of the tables inside the 'mefa' object, it must have a class attribute to be recognised, use e.g. the as.factor function (see 'Examples').

The resulting 'mefa' object must contain at least 2 samples and 2 taxa.

Extractor functions xtab, samp, taxa, and segm extract the corresponding element from the 'mefa' object.

Value

An object of class 'mefa'.

Note

The aggregate method replaces the function strify, the [ method replaces the function exclmf of the old (< 2.0) versions of the mefa package.

Author(s)

P'eter S'olymos, solymos@ualberta.ca

References

S'olymos P. (2008) mefa: an R package for handling and reporting count data. Community Ecology 9, 125–127.

S'olymos P. (2009) Processing ecological data in R with the mefa package. Journal of Statistical Software 29(8), 1–28. http://www.jstatsoft.org/v29/i08/

http://mefa.r-forge.r-project.org/

See Also

mefa

Examples

data(dol.count, dol.samp, dol.taxa)
x <- mefa(stcs(dol.count), dol.samp, dol.taxa)
## These two are identical
aggregate(x, "microhab")
aggregate(x, x$samp$microhab, inside = FALSE)
## Interaction
aggregate(x, c("method", "microhab"))
## Both samples and taxa tables
aggregate(x, "microhab", "familia")
## To use a factor
aggregate(x, "microhab", as.factor(rbinom(dim(x)[2], 1, 0.5)))
## Extraction
x[1:10, ]
x[, 1:5]
x[, , "broken"]
x[1:10, 1:5]
x[1:10, 1:5, 2]
## Note factor levels
y <- x[which(x$samp$method == "time"), ]
levels(y$samp$method)
z <- x[which(x$samp$method == "time"), drop = TRUE]
levels(z$samp$method)
## Note the differences in total sums
y <- mefa(stcs(dol.count), dol.samp, dol.taxa, nested = TRUE)
x[ , , 2]
y[ , , 2]

Results