Learn R Programming

gRbase (version 1.7-5)

tableOpertations: Operations on multidimensional tables (arrays).

Description

A multidimensional table (an array) is here a vector with a dim and a dimnames attribute.

Usage

tabPerm(tab, perm) tabMarg(tab, marg)
tabAdd(tab1, tab2) tabSubt(tab1, tab2) tabMult(tab1, tab2) tabDiv(tab1, tab2) tabDiv0(tab1, tab2)
tabEqual(tab1, tab2, eps=1e-12) tabMarg(tab, marg) tabAlign(tab1, tab2) tabExpand(tab1, tab2) tabListAdd(lst) tabListMult(lst) aperm__(tab, perm)

Arguments

tab, tab1, tab2
Multidimensional arrays.
lst
List of multidimensional arrays (no checking is done)
perm
A vector of indices or dimnames giving the desired permutiation. For tabPerm, it can also be a right hand sided formula.
marg
A vector of indices or dimnames defining the desired marginal. For tabMarg it can also be a right hand sided formula.
eps
Criterion for checking equality of two arrays.

Value

Most functions here return a multidimensional array.

Details

perm in aperm__() can be a vector of indices (as in Rs own aperm()) but also a vector of dimnames. Currently there is no checking that the dimnames are actually in the array, so please take care.

tabDiv0 divides two tables with the convention that 0/0=0.

See Also

aperm

Examples

Run this code
tab1 <- array(1:8, dim=c(2,2,2), dimnames=list("a"=1:2,"b"=1:2,"c"=1:2))
tab2 <- array(1:8, dim=c(2,2,2), dimnames=list("b"=1:2,"c"=1:2,"d"=1:2))

## ## tabMarg ##
## Marginalize down to the bc-array
tabMarg(tab1, 2:3)
tabMarg(tab1, c("b","c"))
tabMarg(tab1, ~b+c)

## This gives an error
## tabMarg(tab1, c(2,5))
## tabMarg(tab1, c("b","w"))
## tabMarg(tab1, ~b+w)


## ## tabPerm ##
tabPerm(tab1, 1:3)      ## No change - an abc-table
tabPerm(tab1, c(2,3,1)) ## A bca-table
tabPerm(tab1, ~b+c+a)

## This gives error
## tabPerm(tab1, c(2,1))
## tabPerm(tab1, c(2,1,5))
## tabPerm(tab1, c(2,1,NA))

## ## tabMult etc ##
## Multiply two arrays
out <- tabMult(tab1, tab2)
out <- tabPerm(out, ~a+b+c+d) ## Just for comparison below
ftable(out)
## Alternative approch
df1 <- as.data.frame.table(tab1)
df2 <- as.data.frame.table(tab2)
df3 <- merge(df1, df2, by=c("b","c"))
df3 <- transform(df3, Freq=Freq.x*Freq.y)
tab3 <- xtabs(Freq ~ a+b+c+d, data=df3)
ftable(tab3)

## ## tabExpand ##
tab1.e <- tabExpand(tab1, tab2)
## tab1.e has dimnames b,c,d,a; values are simply replicated for each
## level of d.
dimnames(tab1.e)
ftable(tab1.e, row.vars="d")

## ## tabAlign ##
tab2.e <- tabExpand(tab2, tab1)
names(dimnames(tab2.e))
names(dimnames(tab1.e))
out <- tabAlign(tab1.e, tab2.e)
names(dimnames(out)) ## Same as tab2.e

## ## tabListAdd, tabListMult ##
lst <- list(tab1, tab2)
tabListAdd( lst )
tabListMult( lst )

Run the code above in your browser using DataLab