Get the Sign of Elements of a Numeric Vector in R Programming – sign() Function
Last Updated :
25 Jun, 2020
sign()
function in R Language is used to find the sign of the elements of the numeric vector provided as argument. It does not work on complex vectors.
Syntax: sign(x)
Parameter:
x represents numeric vector
Returns: 1 for Positive number and -1 for Negative Number
Example 1:
x <- 1
y <- -100
cat ( "Sign of x:" , sign (x), "\n" )
cat ( "Sign of y:" , sign (y))
|
Output:
Sign of x: 1
Sign of y: -1
Example 2:
x <- c (1, 5, 0, -10, 100, -20, 10, -69)
cat ( "Sign of elements of vector x: " , sign (x), "\n" )
|
Output:
Sign of elements of vector x: 1 1 0 -1 1 -1 1 -1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...