File: iapplyTest.R

package info (click to toggle)
r-cran-iterators 1.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: sh: 29; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 962 bytes parent folder | download | duplicates (6)
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
library(iterators)

# test iapply on 3D arrays
test01 <- function() {
  test <- function(actual, it) {
    expected <- nextElem(it)
    checkEquals(expected, actual)
    NULL
  }

  a <- array(1:24, c(2,3,4))

  margins <- list(1, 2, 3,
                  c(1, 2), c(1, 3), c(2, 1), c(2, 3), c(3, 1), c(3, 2),
                  c(1, 2, 3), c(1, 3, 2), c(2, 1, 3), c(2, 3, 1),
                  c(3, 1, 2), c(3, 2, 1))
  for(MARGIN in margins) {
    # cat(sprintf('testing %s\n', paste(MARGIN, collapse=', ')))
    it <- iapply(a, MARGIN)
    apply(a, MARGIN, test, it)
  }
}

# test iapply on matrices
test02 <- function() {
  test <- function(actual, it) {
    expected <- nextElem(it)
    checkEquals(expected, actual)
    NULL
  }

  m <- matrix(1:24, c(6,4))

  margins <- list(1, 2, c(1, 2), c(2, 1))
  for(MARGIN in margins) {
    # cat(sprintf('testing %s\n', paste(MARGIN, collapse=', ')))
    it <- iapply(m, MARGIN)
    apply(m, MARGIN, test, it)
  }
}