Get the Sign of Elements of a Numeric Vector in R Programming – sign() Function

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Create a variable
x <- 1
y <- -100
  
# Check sign
cat("Sign of x:", sign(x), "\n")
cat("Sign of y:", sign(y))

chevron_right


Output:

Sign of x: 1 
Sign of y: -1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector
x <- c(1, 5, 0, -10, 100, -20, 10, -69)
  
# Check Sign
cat("Sign of elements of vector x: ", sign(x), "\n")

chevron_right


Output:

Sign of elements of vector x:  1 1 0 -1 1 -1 1 -1 
My Personal Notes arrow_drop_up

Recommended Posts:


Blockchain Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.