Title: NA-Aware Defaults for Common R Functions
Version: 0.4.0
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-27 14:43:22 UTC; rico
Author: Ulrich Atz ORCID iD [aut, cre, cph]
Maintainer: Ulrich Atz <ulrich.atz@unibocconi.it>
Depends: R (≥ 4.1.0)
Repository: CRAN
Date/Publication: 2026-01-27 16:40: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) [copyright holder]

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 "pairwise.complete.obs".

method

Correlation method: "pearson", "kendall", or "spearman".

...

Additional arguments passed to stats::cor().

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(), max(), range(), pmax(), and pmin() that default to na.rm = TRUE.

Usage

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)

Arguments

...

Numeric or character arguments.

na.rm

Logical. Should missing values be removed? Default TRUE.

all_na

Character. What to do when all values are NA: "error" (default) throws an error, "base" returns what base R does with na.rm = TRUE (e.g., Inf for min(), -Inf for max()), "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

finite

Logical. If TRUE, removes all non-finite values (NA, NaN, Inf, -Inf). Only applies to range(). Default FALSE.

Value

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.

Examples

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)


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_na = NULL, ...)

all(x, na.rm = TRUE, all_na = NULL, ...)

Arguments

x

A logical vector.

na.rm

Logical. Should missing values be removed? Default TRUE.

all_na

Character. What to do when all values are NA: "error" (default) throws an error, "base" returns what base R does with na.rm = TRUE (FALSE for any(), TRUE for all()), "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

...

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. Both return NA for rows where ALL values are missing (base rowMeans() returns NaN, base rowSums() returns 0).

Usage

rowMeans(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...)

rowSums(x, na.rm = TRUE, all_na = NULL, dims = 1L, ...)

Arguments

x

A numeric matrix or data frame.

na.rm

Logical. Should missing values be removed? Default TRUE.

all_na

Character. What to do when all values are NA: "error" (default) throws an error, "base" returns what base R does with na.rm = TRUE (NaN for rowMeans(), 0 for rowSums()), "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

dims

Integer. Number of dimensions to treat as rows.

...

Additional arguments passed to the base function.

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, 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, ...)

Arguments

x

A numeric vector.

na.rm

Logical. Should missing values be removed? Default TRUE.

all_na

Character. What to do when all values are NA: "error" (default) throws an error, "base" returns what base R does with na.rm = TRUE (e.g., NaN for mean(), 0 for sum()), "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

...

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)

# Control all-NA behavior
mean(c(NA, NA), all_na = "na")


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 "ifany".

Value

A contingency table of class table.

Examples

x <- c("a", "b", NA, "a", NA)
table(x)