extract.array: Extract a subset of an array, matrix or a vector with unknown...

Description Usage Arguments Value Author(s) See Also Examples

Description

Extract a subset of an array, matrix or a vector with unknown dimensions.

This method is useful when you do not know the number of dimensions of the object your wish to extract values from, cf. example.

Usage

1
2
## S3 method for class 'array'
extract(x, ..., indices=list(...), dims=names(indices), drop=FALSE)

Arguments

x

An array or a matrix.

...

These arguments are by default put into the indices list.

indices

A list of index vectors to be extracted.

dims

An vector of dimensions - one per element in indices - which will be coerced to integers. If NULL, it will default to seq_along(indices).

drop

If TRUE, dimensions of length one are dropped, otherwise not.

Value

Returns an array.

Author(s)

Henrik Bengtsson

See Also

slice.index()

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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example using an array with a random number of dimensions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
maxdim <- 4
dim <- sample(3:maxdim, size=sample(2:maxdim, size=1), replace=TRUE)
ndim <- length(dim)
dimnames <- list()
for (kk in 1:ndim)
  dimnames[[kk]] <- sprintf("%s%d", letters[kk], 1:dim[kk])
x <- 1:prod(dim)
x <- array(x, dim=dim, dimnames=dimnames)

cat("\nArray 'x':\n")
print(x)


cat("\nExtract 'x[2:3,...]':\n")
print(extract(x, "1"=2:3))

cat("\nExtract 'x[3,2:3,...]':\n")
print(extract(x, "1"=3,"2"=2:3))

cat("\nExtract 'x[...,2:3]':\n")
print(extract(x, indices=2:3, dims=length(dim(x))))



# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Assertions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
y <- array(1:24, dim=c(2,3,4))
yA <- y[,,2:3]
yB <- extract(y, indices=list(2:3), dims=length(dim(y)))
stopifnot(identical(yB, yA))

yA <- y[,2:3,2]
yB <- extract(y, indices=list(2:3,2), dims=c(2,3), drop=TRUE)
stopifnot(identical(yB, yA))

Example output

Loading required package: R.oo
Loading required package: R.methodsS3
R.methodsS3 v1.8.1 (2020-08-26 16:20:06 UTC) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.24.0 (2020-08-26 16:11:58 UTC) successfully loaded. See ?R.oo for help.

Attaching package:R.ooThe following object is masked frompackage:R.methodsS3:

    throw

The following objects are masked frompackage:methods:

    getClasses, getMethods

The following objects are masked frompackage:base:

    attach, detach, load, save

R.utils v2.10.1 (2020-08-26 22:50:31 UTC) successfully loaded. See ?R.utils for help.

Attaching package:R.utilsThe following object is masked frompackage:utils:

    timestamp

The following objects are masked frompackage:base:

    cat, commandArgs, getOption, inherits, isOpen, nullfile, parse,
    warnings


Array 'x':
, , c1, d1

   b1 b2 b3
a1  1  4  7
a2  2  5  8
a3  3  6  9

, , c2, d1

   b1 b2 b3
a1 10 13 16
a2 11 14 17
a3 12 15 18

, , c3, d1

   b1 b2 b3
a1 19 22 25
a2 20 23 26
a3 21 24 27

, , c4, d1

   b1 b2 b3
a1 28 31 34
a2 29 32 35
a3 30 33 36

, , c1, d2

   b1 b2 b3
a1 37 40 43
a2 38 41 44
a3 39 42 45

, , c2, d2

   b1 b2 b3
a1 46 49 52
a2 47 50 53
a3 48 51 54

, , c3, d2

   b1 b2 b3
a1 55 58 61
a2 56 59 62
a3 57 60 63

, , c4, d2

   b1 b2 b3
a1 64 67 70
a2 65 68 71
a3 66 69 72

, , c1, d3

   b1 b2 b3
a1 73 76 79
a2 74 77 80
a3 75 78 81

, , c2, d3

   b1 b2 b3
a1 82 85 88
a2 83 86 89
a3 84 87 90

, , c3, d3

   b1 b2 b3
a1 91 94 97
a2 92 95 98
a3 93 96 99

, , c4, d3

    b1  b2  b3
a1 100 103 106
a2 101 104 107
a3 102 105 108

, , c1, d4

    b1  b2  b3
a1 109 112 115
a2 110 113 116
a3 111 114 117

, , c2, d4

    b1  b2  b3
a1 118 121 124
a2 119 122 125
a3 120 123 126

, , c3, d4

    b1  b2  b3
a1 127 130 133
a2 128 131 134
a3 129 132 135

, , c4, d4

    b1  b2  b3
a1 136 139 142
a2 137 140 143
a3 138 141 144


Extract 'x[2:3,...]':
, , c1, d1

   b1 b2 b3
a2  2  5  8
a3  3  6  9

, , c2, d1

   b1 b2 b3
a2 11 14 17
a3 12 15 18

, , c3, d1

   b1 b2 b3
a2 20 23 26
a3 21 24 27

, , c4, d1

   b1 b2 b3
a2 29 32 35
a3 30 33 36

, , c1, d2

   b1 b2 b3
a2 38 41 44
a3 39 42 45

, , c2, d2

   b1 b2 b3
a2 47 50 53
a3 48 51 54

, , c3, d2

   b1 b2 b3
a2 56 59 62
a3 57 60 63

, , c4, d2

   b1 b2 b3
a2 65 68 71
a3 66 69 72

, , c1, d3

   b1 b2 b3
a2 74 77 80
a3 75 78 81

, , c2, d3

   b1 b2 b3
a2 83 86 89
a3 84 87 90

, , c3, d3

   b1 b2 b3
a2 92 95 98
a3 93 96 99

, , c4, d3

    b1  b2  b3
a2 101 104 107
a3 102 105 108

, , c1, d4

    b1  b2  b3
a2 110 113 116
a3 111 114 117

, , c2, d4

    b1  b2  b3
a2 119 122 125
a3 120 123 126

, , c3, d4

    b1  b2  b3
a2 128 131 134
a3 129 132 135

, , c4, d4

    b1  b2  b3
a2 137 140 143
a3 138 141 144


Extract 'x[3,2:3,...]':
, , c1, d1

   b2 b3
a3  6  9

, , c2, d1

   b2 b3
a3 15 18

, , c3, d1

   b2 b3
a3 24 27

, , c4, d1

   b2 b3
a3 33 36

, , c1, d2

   b2 b3
a3 42 45

, , c2, d2

   b2 b3
a3 51 54

, , c3, d2

   b2 b3
a3 60 63

, , c4, d2

   b2 b3
a3 69 72

, , c1, d3

   b2 b3
a3 78 81

, , c2, d3

   b2 b3
a3 87 90

, , c3, d3

   b2 b3
a3 96 99

, , c4, d3

    b2  b3
a3 105 108

, , c1, d4

    b2  b3
a3 114 117

, , c2, d4

    b2  b3
a3 123 126

, , c3, d4

    b2  b3
a3 132 135

, , c4, d4

    b2  b3
a3 141 144


Extract 'x[...,2:3]':
, , c1, d2

   b1 b2 b3
a1 37 40 43
a2 38 41 44
a3 39 42 45

, , c2, d2

   b1 b2 b3
a1 46 49 52
a2 47 50 53
a3 48 51 54

, , c3, d2

   b1 b2 b3
a1 55 58 61
a2 56 59 62
a3 57 60 63

, , c4, d2

   b1 b2 b3
a1 64 67 70
a2 65 68 71
a3 66 69 72

R.utils documentation built on Sept. 26, 2021, 9:07 a.m.