Operations on multidimensional arrays.

Share:

Description

Operations like marginalize, permute, slicing etc on arrays A multidimensional table (an array) is here a vector with a dim and a dimnames attribute.

Usage

1
2
3
4
5
6
7
8
9
arperm(tab, perm)

armarg(tab, marg)

arequal(tab1, tab2, eps = 1e-12)

arexpand(tab1, tab2)

aralign(tab1, tab2)

Arguments

tab, tab1, tab2

Multidimensional arrays.

perm

A vector of indices or dimnames giving the desired permutiation.

marg

Specification of marginal; either a character vector, a numeric vector or a right hand sided formula For arperm and ararg it can also be a right hand sided formula.

eps

Criterion for checking equality of two arrays.

Details

perm in arperm() 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.

Value

Most functions here return a multidimensional array.

Author(s)

Søren Højsgaard, sorenh@math.aau.dk

See Also

aperm, arslice, arslice_entries

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
ar1 <- array(1:8, dim=c(2,2,2), dimnames=list("a"=1:2,"b"=1:2,"c"=1:2))
ar2 <- array(1:8, dim=c(2,2,2), dimnames=list("b"=1:2,"c"=1:2,"d"=1:2))

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

## This gives an error
## armarg(ar1, c(2,5))
## armarg(ar1, c("b","w"))
## armarg(ar1, ~b + w)
 
## ## arperm ##
arperm(ar1, 1:3)      ## No change - an abc-table
arperm(ar1, c(2,3,1)) ## A bca-table
arperm(ar1, ~b + c + a)

## This gives error
## arperm(ar1, c(2,1))
## arperm(ar1, c(2,1,5))
## arperm(ar1, c(2,1,NA))

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

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

## ## aralign ##
ar2.e <- arexpand(ar2, ar1)
names(dimnames(ar2.e))
names(dimnames(ar1.e))
out <- aralign(ar1.e, ar2.e)
names(dimnames(out)) ## Same as ar2.e

## arexpand:
ar1 <- array(1:8, dim=c(2,2,2), dimnames=list("a"=1:2,"b"=1:2,"c"=1:2))
ar2 <- array(1:8, dim=c(2,2,2), dimnames=list("b"=1:2,"c"=1:2,"d"=1:2))

arexpand(ar1, ar2) %>% ftable(row.vars=1) ## Same as
## arexpand(ar1, dimnames(ar2)) %>% ftable(row.vars=1) 

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.