Row-wise maxima
rowMaxs.RdCompute row-wise maxima from a logical/numeric matrix or data.frame. Optionally ignore missing values. Row names are preserved on ordinary non-empty results.
Arguments
- x
(logical/numeric matrix or data.frame)
A rectangular object whose rows will be aggregated via maxima. Each column must be logical or numeric.- na.rm
(Scalar logical:
FALSE;c(FALSE, TRUE))
IfTRUE, ignoreNAvalues when computing row-wise maxima, returningNAonly when all values in a row are missing. IfFALSE, the presence of anyNAin a row yields anNAresult for that row.
Value
A vector of length nrow(x) containing the row-wise maxima.
The storage type is integer when x is integer or logical, and double otherwise.
If x has zero rows, a zero-length vector of the appropriate storage type is returned.
If x has zero columns, an unnamed length-nrow(x) vector of NA of the appropriate storage type is returned.
Rows return NA according to the rules described by na.rm.
For inputs with at least one row and one column, row names are preserved when present.
Details
Let \(p\) be the number of columns in x.
For row \(i\), define the row-wise maximum \(M_i\) as follows.
If na.rm = TRUE, the maximum is computed over the observed values only.
$$M_i = \max\{x_{ij} : x_{ij} \text{ not NA}\}.$$
NaN is treated as missing because missingness is determined by base::is.na().
If all entries in row \(i\) are missing, the returned value for that row is NA.
If na.rm = FALSE, the maximum includes missing values.
$$M_i = \max\{x_{ij}\}.$$
In this case, any NA present in row \(i\) yields an NA result for that row.
Inf and -Inf are treated as observed values and can be returned as row maxima.
Logical values are evaluated as FALSE = 0 and TRUE = 1.
If x has row names, those names are assigned to the resulting vector.
See also
Other rowwise:
rowMaxs2(),
rowMeans2(),
rowSums2()
Examples
#----------------------------------------------------------------------------
# rowMaxs() examples
#----------------------------------------------------------------------------
library(bkbase)
# Example matrix
m <- matrix(
c(1, NA, 3, 4,
2, 5, NA, 6),
nrow = 2,
byrow = TRUE
)
m
#> [,1] [,2] [,3] [,4]
#> [1,] 1 NA 3 4
#> [2,] 2 5 NA 6
# Row-wise maxima without removing NAs (rows containing NA yield NA)
rowMaxs(m, na.rm = FALSE)
#> [1] NA NA
# Row-wise maxima removing NAs
rowMaxs(m, na.rm = TRUE)
#> [1] 4 6
# Row names are preserved on ordinary non-empty results
rownames(m) <- c("row_a", "row_b")
rowMaxs(m, na.rm = TRUE)
#> row_a row_b
#> 4 6