apply_core: Apply a function over the core data matrix

Description Usage Arguments Value Examples

Description

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

Usage

1
2
3
4
apply_core(x, ...)

## S3 method for class '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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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')

zoocat documentation built on May 2, 2019, 10:22 a.m.