array-operations: Operations on multidimensional arrays.

Description Usage Arguments Details Value Author(s) See Also Examples

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
10
11
12
13
14
15
16
17
18
19
ar_perm(tab, perm)

ar_prod_list(lst)

ar_sum_list(lst)

ar_marg(tab, marg)

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

tab1 %a==% tab2

tab1 %a_% marg

tab1 %a^% extra

ar_expand(tab1, tab2)

ar_align(tab1, tab2)

Arguments

tab, tab1, tab2

Multidimensional arrays.

perm

A vector of indices or dimnames giving the desired permutiation.

lst

List of arrays.

marg

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

eps

Criterion for checking equality of two arrays.

extra

List defining the extra dimensions.

Details

perm in ar_perm() 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, ar_perm, ar_slice, ar_slice_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
58
59
60
61
62
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
ar_marg(ar1, 2:3)
ar_marg(ar1, c("b","c"))
ar_marg(ar1, ~b + c)

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

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

## ## ar_mult etc ##
## Multiply two arrays
out <- ar_mult(ar1, ar2)
out <- ar_perm(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)

## ## ar_expand ##
ar1.e <- ar_expand(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 <- ar_expand(ar2, ar1)
names(dimnames(ar2.e))
names(dimnames(ar1.e))
out <- ar_align(ar1.e, ar2.e)
names(dimnames(out)) ## Same as ar2.e


aa = ar_new(~a, levels=2, values=c(1,100))
ar_expand(aa, list(b=1:2))
aa %a^% list(b=1:2)

## ar_expand:
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))

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


Search within the gRbase package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.