Package 'tidyna'

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

Help Index


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.

...

Additional arguments passed to the base function.

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

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.

...

Additional arguments passed to the base function.

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

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)

NA-aware weighted mean

Description

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.

Usage

weighted.mean(x, w, ..., na.rm = TRUE, all_na = NULL)

Arguments

x

A numeric vector of values.

w

A numeric vector of weights the same length as x.

...

Additional arguments passed to stats::weighted.mean().

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, "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

Value

A length-one numeric vector.

Examples

x <- c(1, 2, NA, 4)
w <- c(1, 1, 1, 1)
weighted.mean(x, w)