Learn R Programming

zoocat (version 0.2.0.1)

apply_core: Apply a function over the core data matrix

Description

Apply a function over the core data matrix of the zoocat object, and bind the return data with cattr or index.

Usage

apply_core(x, ...)

# S3 method for zoocat apply_core(x, FUN, bind, ...)

Arguments

x

the object.

...

other arguments for FUN.

FUN

the function to apply. The FUN must return a matrix or a vector.

bind

a vector of length 1 or 2 with element values to be 'cattr' or 'index' or NA to describe how to bind the return data with cattr or index. If FUN return a vector, set bind to be a scalar. If FUN return a matrix, set bind to be a vector of length 2. See details.

Value

a data frame, a "zoo" object, or a "zoocat" object.

Examples

Run this code
# NOT RUN {
x <- matrix(1 : 20, nrow = 5)
colAttr <- data.frame(month = c(2, 3, 5, 6), name = c(rep('xxx', 3), 'yyy'))
zc <- zoocat(x, order.by = 1991 : 1995, colattr = colAttr)

apply_core(zc, FUN = colMeans, bind = 'cattr')
apply_core(zc, FUN = rowMeans, bind = 'index')
apply_core(zc, FUN = function (x) {x*2}, bind = c('index', 'cattr'))
apply_core(zc, FUN = function (x) {t(x*2)}, bind = c('cattr', 'index'))
apply_core(zc, FUN = function (x) {x*2}, bind = c('index', NA))
apply_core(zc, FUN = function (x) {x[3:4, ]}, bind = c(NA, 'cattr'))
apply_core(zc, FUN = function (x) 
                      {r <- x[3:4, ]
                      rownames(r) <- c('a', 'b')
                      return(r)}, 
                      bind = c(NA, 'cattr'))

vec <- as.vector(zc[, 1])
apply_core(zc, FUN = function (x) {cor(x, vec)}, bind = 'cattr')

# }

Run the code above in your browser using DataLab