How to Check If Number is Positive, Zero, or Negative in R

Integer vectors in R create or tests for objects of type “integer“. To check if the Numbers of vectors are Negative, use the is.negative() function.

Check If Number is Positive, Zero, or Negative in R

To check if the number is positive, negative, or zero in R, use the comparison operators. If the value is greater than 0, then it is positive; if it is less than 0, then negative, and if it is equal to zero(0), then it is 0.

To use the dynamic value in R, use the readline() function.

data <- as.double(readline(prompt = "Enter a number: "))

if (data > 0) {
 print("Positive number")
} else if (data == 0) {
 print("0")
} else {
 print("Negative number")
}

Output

Enter a number: 19
[1] "Positive Number"

Enter a number: -21
[1] "Negative Number"

Enter a number: 0
[1] "0"

So, To check whether a number is positive, negative, or zero. We have solved this problem using if…elif…else and nested if…else statement.

That is it for this tutorial.

 

Leave a Comment