File: rowRanges.R

package info (click to toggle)
r-cran-matrixstats 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,104 kB
  • sloc: ansic: 7,300; sh: 11; makefile: 2
file content (253 lines) | stat: -rw-r--r-- 7,451 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
library("matrixStats")

rowMins_R <- function(x, ..., useNames = TRUE) {
  suppressWarnings({
    res <- apply(x, MARGIN = 1L, FUN = min, ...)
  })
  if (!useNames) names(res) <- NULL
  res
} # rowMins_R()

rowMaxs_R <- function(x, ..., useNames = TRUE) {
  suppressWarnings({
    res <- apply(x, MARGIN = 1L, FUN = max, ...)
  })
  if (!useNames) names(res) <- NULL
  res
} # rowMaxs_R()

rowRanges_R <- function(x, ..., useNames = TRUE) {
  suppressWarnings({
    ans <- t(apply(x, MARGIN = 1L, FUN = range, ...))
  })
  
  # Preserve rownames attribute
  dim <- c(dim(x)[1], 2L)
  if (!isTRUE(all.equal(dim(ans), dim))) {
    dim(ans) <- dim
    rownames <- rownames(x)
    if (!is.null(dimnames)) rownames(ans) <- rownames
  }
  if (!useNames) dimnames(ans) <- NULL
  ans
} # rowRanges_R()


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# With and without some NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
  cat("mode: ", mode, "\n", sep = "")

  for (add_na in c(FALSE, TRUE)) {
    cat("add_na = ", add_na, "\n", sep = "")

    x <- matrix(1:50 + 0.1, nrow = 10L, ncol = 5L)
    if (add_na) {
      x[3:7, c(2, 4)] <- NA_real_
    }
    storage.mode(x) <- mode
    str(x)
    
    # To check names attribute
    dimnames <- list(letters[1:10], LETTERS[1:5])
    
    # Test with and without dimnames on x
    for (setDimnames in c(TRUE, FALSE)) {
      if (setDimnames) dimnames(x) <- dimnames
      else dimnames(x) <- NULL
      # Row/column extremes
      for (na.rm in c(FALSE, TRUE)) {
        # Check names attribute
        for (useNames in c(TRUE, FALSE)) {
          cat("na.rm = ", na.rm, "\n", sep = "")
          
          # Ranges
          cat("range:\n")
          r0 <- rowRanges_R(x, na.rm = na.rm, useNames = useNames)
          r1 <- rowRanges(x, na.rm = na.rm, useNames = useNames)
          r2 <- colRanges(t(x), na.rm = na.rm, useNames = useNames)
          stopifnot(all.equal(r1, r2))
          stopifnot(all.equal(r1, r0))
          
          # Min
          cat("min:\n")
          m0 <- rowMins_R(x, na.rm = na.rm, useNames = useNames)
          m1 <- rowMins(x, na.rm = na.rm, useNames = useNames)
          m2 <- colMins(t(x), na.rm = na.rm, useNames = useNames)
          stopifnot(all.equal(m1, m2))
          stopifnot(all.equal(m1, m0))
          
          # Max
          cat("max:\n")
          m0 <- rowMaxs_R(x, na.rm = na.rm, useNames = useNames)
          m1 <- rowMaxs(x, na.rm = na.rm, useNames = useNames)
          m2 <- colMaxs(t(x), na.rm = na.rm, useNames = useNames)
          stopifnot(all.equal(m1, m2))
          stopifnot(all.equal(m1, m0))
        }
      }
    }
  } # for (add_na ...)
} # for (mode ...)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# All NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
  cat("mode: ", mode, "\n", sep = "")
  x <- matrix(NA_real_, nrow = 10L, ncol = 5L)
  storage.mode(x) <- mode
  str(x)
  
  # Test with and without dimnames on x
  for (setDimnames in c(TRUE, FALSE)) {
    if (setDimnames) dimnames(x) <- dimnames
    else dimnames(x) <- NULL
    for (na.rm in c(FALSE, TRUE)) {
      # Check names attribute
      for (useNames in c(TRUE, FALSE)) {
        cat("na.rm = ", na.rm, "\n", sep = "")
        r0 <- rowRanges_R(x, na.rm = na.rm, useNames = useNames)
        r1 <- rowRanges(x, na.rm = na.rm, useNames = useNames)
        r2 <- colRanges(t(x), na.rm = na.rm, useNames = useNames)
        stopifnot(all.equal(r1, r2))
        stopifnot(all.equal(r1, r0))
      }
    }
  }
} # for (mode ...)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Special cases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Nx0 matrix
x <- matrix(double(0L), nrow = 5L, ncol = 0L)
r0 <- rowRanges_R(x)
#r1 <- rowRanges(x)
#r_truth <- matrix(c(Inf, -Inf), nrow = nrow(x), ncol = 2L, byrow = TRUE)
#stopifnot(all.equal(r1, r_truth))

# 0xN matrix
x <- t(x)
#r1 <- colRanges(x)
#stopifnot(all.equal(r1, r_truth))

# Nx1 matrix
x <- matrix(1:5, nrow = 5L, ncol = 1L)
# To check names attribute
dimnames <- list(letters[1:5], "A")
r1 <- rowRanges(x)
r_truth <- matrix(1:5, nrow = nrow(x), ncol = 2L, byrow = FALSE)
stopifnot(all.equal(r1, r_truth))
# Check names attribute
dimnames(x) <- dimnames
r0 <- rowRanges_R(x, useNames = TRUE)
r1 <- rowRanges(x, useNames = TRUE)
stopifnot(all.equal(r1, r0))
dimnames(x) <- NULL

# 1xN matrix
x <- t(x)
r1 <- colRanges(x)
stopifnot(all.equal(r1, r_truth))
# Check names attribute
dimnames(x) <- list("a", LETTERS[1:5])
r1 <- colRanges(x, useNames = TRUE)
stopifnot(identical(rownames(r1), colnames(x)))


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Additional tests with NA_integer_, NA_real, NaN, -Inf, +Inf
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- matrix(1:12, nrow = 4L, ncol = 3L)

na_list <- list(
  "integer"       = matrix(1:12, nrow = 4L, ncol = 3L),
  "integer w/ NA" = matrix(NA_integer_, nrow = 4L, ncol = 3L),
  "real"          = matrix(as.double(1:12), nrow = 4L, ncol = 3L),
  "real w/ NA"    = matrix(NA_real_, nrow = 4L, ncol = 3L)
)

na <- na_list[["real"]]
na[2, 2] <- NA
na_list[["real + NA cell"]] <- na

na <- na_list[["real"]]
na[2, ] <- NA
na_list[["real + NA row"]] <- na

na <- na_list[["real"]]
na[2, ] <- NaN
na_list[["real + NaN row"]] <- na

na <- na_list[["real"]]
na[2, 2] <- Inf
na_list[["real + Inf cell"]] <- na

na <- na_list[["real"]]
na[2, ] <- Inf
na_list[["real + Inf row"]] <- na

na <- na_list[["real"]]
na[2, 2] <- NaN
na_list[["real + NaN cell"]] <- na

na <- na_list[["real w/ NA"]]
na[2, 2] <- NaN
na_list[["real w/ NA + NaN cell"]] <- na

na <- na_list[["real w/ NA"]]
na[2, ] <- NaN
na_list[["real w/ NA + NaN row"]] <- na

# To check names attribute
dimnames <- list(letters[1:4], LETTERS[1:3])

# Test with and without dimnames on x
for (setDimnames in c(TRUE, FALSE)) {
  if (setDimnames) dimnames(x) <- dimnames
  else dimnames(x) <- NULL
  for (na.rm in c(FALSE, TRUE)) {
    for (name in names(na_list)) {
      # Check names attribute
      for (useNames in c(TRUE, FALSE)) {
        na <- na_list[[name]]
        cat(sprintf("%s (%s) w/ na.rm = %s:\n", name, typeof(na), na.rm))
        print(na)
        
        cat("  min:\n")
        y0 <- rowMins_R(na, na.rm = na.rm, useNames = useNames)
        str(y0)
        y1 <- rowMins(na, na.rm = na.rm, useNames = useNames)
        str(y1)
        stopifnot(all.equal(y1, y0))
        y1c <- colMins(t(na), na.rm = na.rm, useNames = useNames)
        str(y1c)
        stopifnot(all.equal(y1c, y1))
        
        cat("  max:\n")
        y0 <- rowMaxs_R(na, na.rm = na.rm, useNames = useNames)
        str(y0)
        y1 <- rowMaxs(na, na.rm = na.rm, useNames = useNames)
        str(y1)
        stopifnot(all.equal(y1, y0))
        y1c <- colMaxs(t(na), na.rm = na.rm, useNames = useNames)
        str(y1c)
        stopifnot(all.equal(y1c, y1))
        
        cat("  range:\n")
        y0 <- rowRanges_R(na, na.rm = na.rm, useNames = useNames)
        str(y0)
        y1 <- rowRanges(na, na.rm = na.rm, useNames = useNames)
        str(y1)
        stopifnot(all.equal(y1, y0))
        y1c <- colRanges(t(na), na.rm = na.rm, useNames = useNames)
        str(y1c)
        stopifnot(all.equal(y1c, y1))
      }
    } # for (name ...)
  } # for (na.rm ...)
}