| Title: | NA-Aware Defaults for Common R Functions |
| Version: | 0.1.2 |
| 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'. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Imports: | cli (≥ 3.0.0) |
| Suggests: | testthat (≥ 3.0.0), withr |
| Config/testthat/edition: | 3 |
| URL: | https://github.com/statzhero/tidyna |
| BugReports: | https://github.com/statzhero/tidyna/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-01-07 14:28:34 UTC; rico |
| Author: | Ulrich Atz |
| Maintainer: | Ulrich Atz <ulrich.atz@unibocconi.it> |
| Repository: | CRAN |
| Date/Publication: | 2026-01-08 19:20:02 UTC |
tidyna: 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'.
Author(s)
Maintainer: Ulrich Atz ulrich.atz@unibocconi.it (ORCID)
See Also
Useful links:
NA-aware Correlation Function
Description
Drop-in replacement for cor() that defaults to
use = "pairwise.complete.obs".
Usage
cor(
x,
y = NULL,
use = "pairwise.complete.obs",
method = c("pearson", "kendall", "spearman"),
...
)
Arguments
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 |
Value
A correlation matrix or single correlation coefficient.
Examples
x <- c(1, 2, NA, 4)
y <- c(2, 4, 6, 8)
cor(x, y)
NA-aware Extrema Functions
Description
Drop-in replacements for min() and max() that default to na.rm = TRUE.
Usage
min(..., na.rm = TRUE)
max(..., na.rm = TRUE)
Arguments
... |
Numeric or character arguments. |
na.rm |
Logical. Should missing values be removed? Default |
Value
A length-one vector.
Examples
x <- c(1, NA, 5, 3)
min(x)
max(x)
# Multiple arguments
min(c(5, NA), c(1, 2))
NA-aware Logical Functions
Description
Drop-in replacements for any() and all() that default to na.rm = TRUE.
Usage
any(x, na.rm = TRUE, ...)
all(x, na.rm = TRUE, ...)
Arguments
x |
A numeric vector. |
na.rm |
Logical. Should missing values be removed? Default |
... |
Additional arguments passed to the base function. |
Value
A single logical value.
Examples
x <- c(TRUE, NA, FALSE)
any(x)
all(x)
NA-aware Row-wise Functions
Description
Drop-in replacements for rowMeans() and rowSums() that default to
na.rm = TRUE. Importantly, rowSums() returns NA for rows where
ALL values are missing.
Usage
rowMeans(x, na.rm = TRUE, ...)
rowSums(x, na.rm = TRUE, dims = 1L, ...)
Arguments
x |
A numeric matrix or data frame. |
na.rm |
Logical. Should missing values be removed? Default |
... |
Additional arguments passed to the base function. |
dims |
Integer. Number of dimensions to treat as rows. |
Value
A numeric or complex array of suitable size, or a vector if the result is one-dimensional.
Examples
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)
NA-aware Summary Functions
Description
Drop-in replacements for summary functions that default to na.rm = TRUE
and warn when missing values are removed.
Usage
mean(x, na.rm = TRUE, ...)
sum(x, na.rm = TRUE, ...)
prod(x, na.rm = TRUE, ...)
sd(x, na.rm = TRUE, ...)
var(x, na.rm = TRUE, ...)
median(x, na.rm = TRUE, ...)
quantile(x, na.rm = TRUE, ...)
Arguments
x |
A numeric vector. |
na.rm |
Logical. Should missing values be removed? Default |
... |
Additional arguments passed to the base function. |
Value
The computed summary statistic.
Examples
x <- c(1, 2, NA, 4)
mean(x)
# Suppress warnings
options(tidyna.warn = FALSE)
mean(x)
options(tidyna.warn = TRUE)
# Use base behavior
mean(x, na.rm = FALSE)
NA-aware Table Function
Description
Drop-in replacement for table() that defaults to useNA = "ifany",
showing NA counts when present.
Usage
table(..., useNA = "ifany")
Arguments
... |
Objects to cross-tabulate. |
useNA |
Whether to include NA values. Default |
Value
A contingency table of class table.
Examples
x <- c("a", "b", NA, "a", NA)
table(x)