Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sign

Sign function with zero option


Description

sign returns a vector with the signs of the corresponding elements of x, being 1, zero, or -1 if the number is positive, zero or negative, respectively.

This generalizes the sign function in the base package to allow something other than 0 as the the "sign" of 0.

Usage

sign(x, zero=0L)

Arguments

x

a numeric vector for which signs are desired

zero

an integer value to be assigned for x==0.

Value

an integer vector of the same length as x assuming values 1, zero and -1, as discussed above.

See Also

Examples

##
## 1.  default
##
sx <- sign((-2):2)

# check 

all.equal(sx, base::sign((-2):2))


##
## 2.  with zero = 1
##
s1 <- sign((-2):2, 1)

# check 

all.equal(s1, rep(c(-1, 1), c(2,3)))

Ecfun

Functions for 'Ecdat'

v0.2-4
GPL (>= 2)
Authors
Spencer Graves <spencer.graves@effectivedefense.org>
Initial release
2020-10-26

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.