-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy patharray-subset.R
86 lines (69 loc) · 2.3 KB
/
array-subset.R
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## array subsetting tests
##
## Tests should be written to raise an error on test failure
##
## Test for subsetting of an array using a matrix with ncol == length(dim(x))
## first matrix case
m <- matrix(1:25, ncol=5, dimnames = list(letters[1:5], LETTERS[1:5]))
si <- matrix(c(1, 1, 2, 3, 3, 4), ncol = 2, byrow = TRUE)
ss <- matrix(c("a", "A", "b", "C", "c", "D"), ncol = 2, byrow = TRUE)
stopifnot(identical(m[si], m[ss]))
stopifnot(identical(c(1L, 12L, 18L), m[ss]))
## test behavior of NA entries in subset matrix.
## NA in character matrix should propagate and should not
## match an NA in a dimname.
## An NA in either column propagates to result
ssna <- ss; ssna[2, 2] <- NA
stopifnot(identical(c(1L, NA, 18L), m[ssna]))
ssna <- ss; ssna[2, 1] <- NA
stopifnot(identical(c(1L, NA, 18L), m[ssna]))
## An NA in row/column names is not matched
mnadim <- m
tmp <- rownames(mnadim)
tmp[5] <- NA
rownames(mnadim) <- tmp
stopifnot(identical(c(1L, NA, 18L), m[ssna]))
## Unmatched subscripts raise an error
ssnm <- ss
ssnm[2, 2] <- "NOMATCH"
stopifnot(inherits(try(m[ssnm], silent=TRUE), "try-error"))
## "" does not match and so raises an error
mnadim <- m
tmp <- rownames(mnadim)
tmp[5] <- ""
rownames(mnadim) <- tmp
ssnm <- ss
ssnm[2, 2] <- ""
stopifnot(inherits(try(mnadim[ssnm], silent=TRUE), "try-error"))
## test assignment
m3 <- m2 <- m
m2[si] <- c(100L, 200L, 300L)
m3[ss] <- c(100L, 200L, 300L)
stopifnot(identical(m2, m3))
## now an array case
a <- array(1:75, dim = c(5, 5, 3),
dimnames = list(letters[1:5], LETTERS[1:5], letters[24:26]))
si <- matrix(c(1, 1, 1,
2, 3, 1,
3, 4, 1,
5, 1, 3),
ncol = 3, byrow = TRUE)
ss <- matrix(c("a", "A", "x",
"b", "C", "x",
"c", "D", "x",
"e", "A", "z"),
ncol = 3, byrow = TRUE)
stopifnot(identical(a[si], a[ss]))
stopifnot(identical(c(1L, 12L, 18L, 55L), a[ss]))
a2 <- a1 <- a
a1[si] <- c(100L, 1200L, 1800L, 5500L)
a2[ss] <- c(100L, 1200L, 1800L, 5500L)
stopifnot(identical(a1, a2))
## it is an error to subset if some dimnames are missing NOTE: this
## gives a subscript out of bounds error, might want something more
## informative?
a3 <- a
dn <- dimnames(a3)
dn[2] <- list(NULL)
dimnames(a3) <- dn
stopifnot(inherits(try(a3[ss], silent=TRUE), "try-error"))