| Title: | NA-Aware Defaults for Common R Functions |
|---|---|
| Description: | Provides drop-in replacements for common R functions (mean(), sum(), sd(), min(), etc.) that default to 'na.rm = TRUE' and issue warnings when missing values are removed. It handles some special cases. The table() default is set to 'useNA = ifany'. |
| Authors: | Ulrich Atz [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-1719-3780>) |
| Maintainer: | Ulrich Atz <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.0 |
| Built: | 2026-05-29 17:00:20 UTC |
| Source: | https://github.com/statzhero/tidyna |
Drop-in replacement for cor() that defaults to
use = "pairwise.complete.obs".
cor( x, y = NULL, use = "pairwise.complete.obs", method = c("pearson", "kendall", "spearman"), ... )cor( x, y = NULL, use = "pairwise.complete.obs", method = c("pearson", "kendall", "spearman"), ... )
x |
A numeric vector, matrix, or data frame. |
y |
Optional. A numeric vector, matrix, or data frame. |
use |
Method for handling missing values.
Default |
method |
Correlation method: "pearson", "kendall", or "spearman". |
... |
Additional arguments passed to |
A correlation matrix or single correlation coefficient.
x <- c(1, 2, NA, 4) y <- c(2, 4, 6, 8) cor(x, y)x <- c(1, 2, NA, 4) y <- c(2, 4, 6, 8) cor(x, y)
Drop-in replacements for min(), max(), range(), pmax(), and pmin()
that default to na.rm = TRUE.
min(..., na.rm = TRUE, all_na = NULL) max(..., na.rm = TRUE, all_na = NULL) range(..., na.rm = TRUE, all_na = NULL, finite = FALSE) pmax(..., na.rm = TRUE, all_na = NULL) pmin(..., na.rm = TRUE, all_na = NULL)min(..., na.rm = TRUE, all_na = NULL) max(..., na.rm = TRUE, all_na = NULL) range(..., na.rm = TRUE, all_na = NULL, finite = FALSE) pmax(..., na.rm = TRUE, all_na = NULL) pmin(..., na.rm = TRUE, all_na = NULL)
... |
Numeric or character arguments. |
na.rm |
Logical. Should missing values be removed? Default |
all_na |
Character. What to do when all values are NA:
|
finite |
Logical. If |
For min() and max(), a length-one vector. For range(), a
length-two vector containing the minimum and maximum. For pmax() and
pmin(), a vector of length equal to the longest input.
x <- c(1, NA, 5, 3) min(x) max(x) range(x) # Multiple arguments min(c(5, NA), c(1, 2)) # Parallel max/min pmax(c(1, 5, 3), c(2, 1, 4)) pmin(c(1, NA, 3), c(NA, NA, 1)) # range with infinite values y <- c(1, Inf, 3, -Inf) range(y) range(y, finite = TRUE)x <- c(1, NA, 5, 3) min(x) max(x) range(x) # Multiple arguments min(c(5, NA), c(1, 2)) # Parallel max/min pmax(c(1, 5, 3), c(2, 1, 4)) pmin(c(1, NA, 3), c(NA, NA, 1)) # range with infinite values y <- c(1, Inf, 3, -Inf) range(y) range(y, finite = TRUE)
Drop-in replacements for any() and all() that default to na.rm = TRUE.
any(x, ..., na.rm = TRUE, all_na = NULL) all(x, ..., na.rm = TRUE, all_na = NULL)any(x, ..., na.rm = TRUE, all_na = NULL) all(x, ..., na.rm = TRUE, all_na = NULL)
x |
A logical vector. |
... |
Additional arguments passed to the base function. |
na.rm |
Logical. Should missing values be removed? Default |
all_na |
Character. What to do when all values are NA:
|
A single logical value.
x <- c(TRUE, NA, FALSE) any(x) all(x)x <- c(TRUE, NA, FALSE) any(x) all(x)
Drop-in replacements for rowMeans() and rowSums() that default to
na.rm = TRUE. Both return NA for rows where ALL values are missing
(base rowMeans() returns NaN, base rowSums() returns 0).
rowMeans(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...) rowSums(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...)rowMeans(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...) rowSums(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...)
x |
A numeric matrix or data frame. |
na.rm |
Logical. Should missing values be removed? Default |
all_na |
Character. What to do when all values are NA:
|
dims |
Integer. Number of dimensions to treat as rows. |
... |
Additional arguments passed to the base function. |
A numeric or complex array of suitable size, or a vector if the result is one-dimensional.
mat <- matrix(c(1, NA, 3, NA, NA, NA), nrow = 2, byrow = TRUE) rowSums(mat) # Compare to base R: base::rowSums(mat, na.rm = TRUE)mat <- matrix(c(1, NA, 3, NA, NA, NA), nrow = 2, byrow = TRUE) rowSums(mat) # Compare to base R: base::rowSums(mat, na.rm = TRUE)
Drop-in replacements for summary functions that default to na.rm = TRUE
and warn when missing values are removed.
mean(x, ..., na.rm = TRUE, all_na = NULL) sum(x, ..., na.rm = TRUE, all_na = NULL) prod(x, ..., na.rm = TRUE, all_na = NULL) sd(x, ..., na.rm = TRUE, all_na = NULL) var(x, ..., na.rm = TRUE, all_na = NULL) median(x, ..., na.rm = TRUE, all_na = NULL) quantile(x, ..., na.rm = TRUE, all_na = NULL)mean(x, ..., na.rm = TRUE, all_na = NULL) sum(x, ..., na.rm = TRUE, all_na = NULL) prod(x, ..., na.rm = TRUE, all_na = NULL) sd(x, ..., na.rm = TRUE, all_na = NULL) var(x, ..., na.rm = TRUE, all_na = NULL) median(x, ..., na.rm = TRUE, all_na = NULL) quantile(x, ..., na.rm = TRUE, all_na = NULL)
x |
A numeric vector. |
... |
Additional arguments passed to the base function. |
na.rm |
Logical. Should missing values be removed? Default |
all_na |
Character. What to do when all values are NA:
|
The computed summary statistic.
x <- c(1, 2, NA, 4) mean(x) # Suppress warnings options(tidyna.warn = FALSE) mean(x) options(tidyna.warn = TRUE) # Control all-NA behavior mean(c(NA, NA), all_na = "na")x <- c(1, 2, NA, 4) mean(x) # Suppress warnings options(tidyna.warn = FALSE) mean(x) options(tidyna.warn = TRUE) # Control all-NA behavior mean(c(NA, NA), all_na = "na")
Drop-in replacement for table() that defaults to useNA = "ifany",
showing NA counts when present.
table(..., useNA = "ifany")table(..., useNA = "ifany")
... |
Objects to cross-tabulate. |
useNA |
Whether to include NA values. Default |
A contingency table of class table.
x <- c("a", "b", NA, "a", NA) table(x)x <- c("a", "b", NA, "a", NA) table(x)
Drop-in replacement for stats::weighted.mean() that defaults to
na.rm = TRUE and warns when missing values are removed.
Unlike base R, missing values in either x or w cause the
corresponding pair to be removed.
weighted.mean(x, w, ..., na.rm = TRUE, all_na = NULL)weighted.mean(x, w, ..., na.rm = TRUE, all_na = NULL)
x |
A numeric vector of values. |
w |
A numeric vector of weights the same length as |
... |
Additional arguments passed to |
na.rm |
Logical. Should missing values be removed? Default |
all_na |
Character. What to do when all values are NA:
|
A length-one numeric vector.
x <- c(1, 2, NA, 4) w <- c(1, 1, 1, 1) weighted.mean(x, w)x <- c(1, 2, NA, 4) w <- c(1, 1, 1, 1) weighted.mean(x, w)