Positive or Negative in R

Positive or Negative in R, we will learn how to use the sign function in R to determine whether a numeric value is positive or negative.

The sign function returns the signs of numeric elements, with a value of 1 for positive numbers, 0 for zero, and -1 for negative numbers.

Basic R Syntax

sign(5)           # Basic R syntax of sign function

Example: Return whether Number is Positive or Negative

We can use the sign function to check whether a number is positive or negative.

For example, let’s create a vector of numbers from -2 to 5 and apply the sign function:

sign(-2:5)        # Apply sign function
# -1 -1  0  1  1  1  1  1

This returns the signs of the elements in the vector.

How to Display Percentages on Histogram in R » Data Science Tutorials

The first two elements are negative (indicated by -1), the third element is zero (indicated by 0), and the remaining elements are positive (indicated by +1).

Example: Checking Multiple Numbers

We can also use the sign function to check multiple numbers at once.

For example, let’s create a vector of numbers and apply the sign function:

numbers <- c(-3, 0, 2, -5, 7)
sign(numbers)     # Apply sign function to multiple numbers
# -1  0  1 -1  1

This returns the signs of the elements in the vector.

The first element is negative (indicated by -1), the second element is zero (indicated by 0), the third element is positive (indicated by +1), and the fourth element is negative (indicated by -1), and the fifth element is positive (indicated by +1).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

eleven − one =

Ads Blocker Image Powered by Code Help Pro

Quality articles need supporters. Will you be one?

You currently have an Ad Blocker on.

Please support FINNSTATS.COM by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO