Skip to contents

Return the given object or a default value if the object is 0 (zero)

Usage

defaultIfZero(x, default, count = FALSE)

Arguments

x

vector possibly containing zeroes

default

value to be used instead of zero

count

if TRUE (the default is FALSE) the number of replaced values is returned in the attributes count

Value

x if x is not 0 and default otherwise. The returned object has an attribute count containing the number of values that have been set to the default value.

Examples

defaultIfZero(c(1, 2, 0, 4, 5, 0, 6), NA) # returns the default value (NA) 
#> [1]  1  2 NA  4  5 NA  6
(out <- defaultIfZero(1:6, NA, count = TRUE)) # returns the "actual" values
#> [1] 1 2 3 4 5 6
#> attr(,"count")
#> [1] 0

# The number of values that were zero is returned in the attribute "count" 
attr(out, "count")
#> [1] 0