Skip to contents

Compute row-wise maxima from a logical/numeric matrix or data.frame. Rows exceeding a specified missing-value proportion are set to NA. Row names are preserved on ordinary non-empty results.

Usage

rowMaxs2(x, na_allowed = 0.2)

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_allowed

(Scalar numeric: 0.2; [0, 1])
Maximum allowed proportion of missing values per row (\(\pi_i\)) before the result is set to NA. Rows with \(\pi_i > \text{na\_allowed}\) return NA. Rows with \(\pi_i \leq \text{na\_allowed}\) are computed using observed values only.

Value

A vector of length nrow(x) containing the row-wise maxima, with NA where the missing proportion exceeds na_allowed. 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. For inputs with at least one row and one column, row names are preserved when present.

Details

For row \(i\), let \(\pi_i\) be the proportion of missing values in that row.

If \(\pi_i > \tau\) where \(\tau\) is na_allowed, the returned maximum for row \(i\) is NA. Otherwise, the maximum is computed over the observed values only:

$$M_i = \max\{x_{ij} : x_{ij} \text{ not NA}\}.$$

If all entries in a row are NA, the result for that row is NA regardless of na_allowed. NaN is treated as missing because missingness is determined by base::is.na(). It counts toward \(\pi_i\) and is excluded from the maximum. 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

Examples

#----------------------------------------------------------------------------
# rowMaxs2() 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

# Allow up to 25% missing per row: both rows are computed
rowMaxs2(m, na_allowed = 0.25)
#> [1] 4 6

# Tighten allowance to 0%: any row with an NA becomes NA
rowMaxs2(m, na_allowed = 0)
#> [1] NA NA

# Row names are preserved on ordinary non-empty results
rownames(m) <- c("row_a", "row_b")
rowMaxs2(m, na_allowed = 0.5)
#> row_a row_b 
#>     4     6