apply_core: Apply a function over the core data matrix

Description Usage Arguments Details Value Examples

View source: R/apply_core.R

Description

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

Usage

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

## S3 method for class 'zoocat'
apply_core(x, FUN, bind = NULL, ...)

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 returned 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.

Details

If FUN return a vector, bind can be one of:

  1. "cattr": In this case, the vector will be combined with the cattr to return a data frame.

  2. "index": In this case, the vector will be combined with the index to return a "zoo" object.

If FUN return a matrix named x.ret, bind can be one of:

  1. c("index", "cattr"): In this case, the rows of x.ret will be conbined with the index, and the columns of x.ret will be combined with the cattr. So a "zoocat" object will be returned.

  2. c("cattr", "index"): In this case, the rows of x.ret will be combined with the cattr, and the columns of x.ret will be combined with the index. So a "zoocat" object will be returned, but the core data in the "zoocat" object will be t(x.ret).

  3. c("cattr", NA): In this case, the rows of x.ret will be combined with the cattr to return a data frame.

  4. c(NA, "cattr"): In this case, the columns of x.ret will be combined with the cattr to return a data frame.

  5. c("index", NA): In this case, the rows of x.ret will be combined with the index to return a "zoo" object.

  6. c(NA, "index"): In this case, the columns of x.ret will be combined with the index to return a "zoo" object.

Note that if bind is NULL (default), the original returned value of FUN will be returned.

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
19
20
21
22
23
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)
apply_core(zc, FUN = colMeans, bind = 'cattr')

apply_core(zc, FUN = rowMeans)
apply_core(zc, FUN = rowMeans, bind = 'index')

apply_core(zc, FUN = function (x) {x*2})
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) {as.vector(cor(x, vec))}, bind = 'cattr')

ran-ran/zoocat documentation built on July 5, 2017, 8:41 p.m.