Skip to contents

Compute row-wise sums from a logical/numeric matrix or data.frame. Rows exceeding a specified missing-value proportion are set to NA. Optionally impute missing values using the row mean when the proportion of missing values does not exceed the specified threshold.

Usage

rowSums2(x, na_allowed = 0.2, mean_impute = FALSE, round_digits = NULL)

Arguments

x

(logical/numeric matrix or data.frame)
A rectangular object whose rows will be aggregated via sums. 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 \le \text{na\_allowed}\) are computed.

mean_impute

(Scalar logical: FALSE; c(FALSE, TRUE))
Let \(\pi_i\) be the proportion of missing values in rows. If TRUE, rows with at least one missing value and \(\pi_i \le \text{na\_allowed}\) are imputed using the row mean scaled by the number of columns. If FALSE, sums are computed over observed values only.

round_digits

(NULL or scalar non-negative integer: NULL)
If not NULL, round the resulting sums to the specified number of digits.

Value

A double vector of length nrow(x) containing the row-wise sums, with NA where the missing proportion exceeds na_allowed. For inputs with at least one row and one column, row names are preserved. If x has zero rows, a zero-length double vector is returned. If x has zero columns, an unnamed length-nrow(x) double vector of NA is returned.

Details

Let \(p\) be the number of columns in x. 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 sum for row \(i\) is NA. Otherwise:

$$S_i = \sum_{j: x_{ij} \text{ not NA}} x_{ij}$$ when mean_impute = FALSE. $$S_i = p \cdot \bar{x}_{i,\text{obs}}$$ when mean_impute = TRUE, where \(\bar{x}_{i,\text{obs}}\) is the mean of the non-missing values in row \(i\).

If all entries in a row are NA or NaN, the row has no observed mean. With mean_impute = TRUE, such rows return NA even when na_allowed = 1. With mean_impute = FALSE, such rows return 0 when allowed by na_allowed, matching base::rowSums() with na.rm = TRUE. NaN is treated as missing because missingness is determined by base::is.na(). Inf and -Inf are treated as observed values and are passed to base::rowSums() arithmetic. A row with both observed Inf and -Inf can therefore return NaN. Logical values are summed as FALSE = 0 and TRUE = 1.

Rounding is disabled by default. Set round_digits to round the resulting sums.

See also

Examples

#----------------------------------------------------------------------------
# rowSums2() examples
#----------------------------------------------------------------------------
library(bkbase)

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

# No imputation, allow up to 25% missing per row
rowSums2(m, na_allowed = 0.25, mean_impute = FALSE)
#> [1]  8 13

# Mean imputation for rows with some missing but within threshold
rowSums2(m, na_allowed = 0.5, mean_impute = TRUE)
#> [1] 10.66667 17.33333

# Enable rounding to 0 digits
rowSums2(m, na_allowed = 0.5, mean_impute = TRUE, round_digits = 0)
#> [1] 11 17