abind: Bind comformable arrays

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

View source: R/fun.R

Description

Bind comformable arrays

Usage

1
2
3
abind(arr1, arr2, d, facename = "")

bind.rdc(arr1, arr2, d, facename = "")

Arguments

arr1

first array

arr2

second array

d

dimension along which arr1 and arr2 are joined, or, if the length(dim(arr1)) == length(dim(arr2)) + 1 the dimension in arr1 that is extended by arr2.

facename

Name for the new array dimension

Details

abind binds two conformable arrays along a dimension. abind.rdc is a version used in the RDC.

dim( arr1 ) and dim( arr2 ) must be equal except in the dth dimension. If the length of dim( arr2 ) is 1 less than that of dim( arr1 ), then 'arr2' is treated as if it had dropped the dth dimension with size 1.

Value

The returned value, ret, is an array with dimension dim(arr1) except for the dth dimension where dim(ret)[d] == dim(arr1)[d] + dim(arr2)[d].

If length(dim(arr2)) == length(dim(arr1)) - 1, then arr2 is treated as if it dropped a dimension of size 1 in the dth dimension. 'facename' is used as the name of the dimnames list for this dimension.

Author(s)

Georges Monette

See Also

aperm, to permute arrays

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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (arr1, arr2, d, facename = "") 
{
    d1 <- dim(arr1)
    n1 <- length(d1)
    dn1 <- dimnames(arr1)
    d2 <- dim(arr2)
    n2 <- length(d2)
    dn2 <- dimnames(arr2)
    arenull <- is.null(dn1) & is.null(dn2)
    if (is.null(dn1)) {
        dn1 <- lapply(as.list(d1), function(x) seq(1, x))
        dimnames(arr1) <- dn1
    }
    if (n1 != n2) {
        d2 <- d1
        d2[d] <- 1
        dn2 <- dn1
        dn2[[d]] <- facename
        dimnames(arr2) <- NULL
        dim(arr2) <- d2
        dimnames(arr2) <- dn2
        n2 <- n1
    }
    if (is.null(dn2)) {
        dn2 <- lapply(as.list(d2), function(x) seq(1, x))
        dimnames(arr2) <- dn2
    }
    perm <- 1:n1
    perm[c(d, n1)] <- c(n1, d)
    arr.p1 <- aperm(arr1, perm)
    arr.p2 <- aperm(arr2, perm)
    dret <- d1[perm]
    dret[n1] <- dret[n1] + d2[d]
    dnret <- dn1
    dnret[[d]] <- c(dnret[[d]], dn2[[d]])
    ret <- c(arr.p1, arr.p2)
    dim(ret) <- dret
    ret <- aperm(ret, perm)
    dimnames(ret) <- dnret
    ret
  }

gmonette/spida documentation built on May 14, 2017, 1 p.m.