Rev 54810 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
## 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 casem <- 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 resultssna <- ss; ssna[2, 2] <- NAstopifnot(identical(c(1L, NA, 18L), m[ssna]))ssna <- ss; ssna[2, 1] <- NAstopifnot(identical(c(1L, NA, 18L), m[ssna]))## An NA in row/column names is not matchedmnadim <- mtmp <- rownames(mnadim)tmp[5] <- NArownames(mnadim) <- tmpstopifnot(identical(c(1L, NA, 18L), m[ssna]))## Unmatched subscripts raise an errorssnm <- ssssnm[2, 2] <- "NOMATCH"stopifnot(inherits(try(m[ssnm], silent=TRUE), "try-error"))## "" does not match and so raises an errormnadim <- mtmp <- rownames(mnadim)tmp[5] <- ""rownames(mnadim) <- tmpssnm <- ssssnm[2, 2] <- ""stopifnot(inherits(try(mnadim[ssnm], silent=TRUE), "try-error"))## test assignmentm3 <- m2 <- mm2[si] <- c(100L, 200L, 300L)m3[ss] <- c(100L, 200L, 300L)stopifnot(identical(m2, m3))## now an array casea <- 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 <- aa1[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 <- adn <- dimnames(a3)dn[2] <- list(NULL)dimnames(a3) <- dnstopifnot(inherits(try(a3[ss], silent=TRUE), "try-error"))