Skip to contents

Returns NA_real_ if all values are NA; otherwise returns the sum with missing values treated as zero.

Usage

safe_sum(x)

Arguments

x

A numeric vector.

Value

A single numeric value: NA_real_ if all values in x are missing, otherwise the numeric sum with NAs ignored.

Details

This is useful in dplyr::summarise() calls where you want a sum that respects full-missing groups by yielding NA_real_, and otherwise sums while ignoring NAs.

Examples

# All missing -> NA
sntutils:::`safe_sum`(c(NA, NA))
#> [1] NA

# Mixed missing -> sum of non-missing
sntutils:::`safe_sum`(c(1, NA, 2))
#> [1] 3

# No missing -> normal sum
sntutils:::`safe_sum`(c(1, 2, 3))
#> [1] 6