R

# Less Than or Equal To in R

“The “<=” is a type of relational operator. Relational operators are used in a programming language to perform different operations that check or establish a relationship between two elements. Numerical equality (e.g., A = A) and inequality (e.g., 5 >2) are examples of these. The relational operators are mostly used to generate a test expression that regulates the flow of the program. Six relational operators compare (display the relationship) two operands to get a Boolean value. If the data types of operands are different, implicit promotion takes place to transform them into the same data type.”

## How to Use Less Than or Equal Operators in R Programming Language in Ubuntu 20.04?

A relation operator describes the relationship between two values or the comparison of two values or operands. ( <= ) Less than or Equal to is the combination of two relational operators (“<” and “=”) with an OR operator between them, so it will give an output of true whenever the condition is satisfied.

Syntax: first_operand <= second_operand

These operands can be numerical values, characters, vectors, and matrices.

In the following example, we will try to demonstrate in how many ways and for what reasons you can use the less than or equal operator in R.

## Example # 1: (<=) operator With Numeric Values in R

In this instance, we will take two values as operands. The first operand will be equated to the second operand to see if it is less than or equal to it or not. Let’s create two variables and assign a numeric value to them.

We have specified a= 10 and b =20. As this will create an expression 10<=20, it will return true because it doesn’t matter whether “a” is equal to b or not, but b is less than a. So, the condition is satisfied.

As a=20 and b are also 20, so it will give an output of true.

In the overhead case, the value of “a” is neither less nor equal to b. The expression we will get 30<=20 is false.

## Example # 2: (<=) operator With Characters in R

We will create two character variables to check the less than or equal relationship between them. Let’s try with character variables a and b.

As in both variables, the characters are the same, i.e., “computer.” It will generate an expression of “computer”<=” computer,” which is true.

In the above case, a is not equal to b (comp is not equal to computer). However, the <= operator will calculate each letter and determines that a<b, so the output will be TRUE.

The value of “a” is specified as “mouse” and b as “computer.” Both of the characters are entirely different; the condition will not satisfy them. It will give a FALSE output.

## Example # 3: (<=) operator With Vectors in R

Vectors With Equal Length

In this example, we will create two vectors to determine the less than or equal relationship between them. Let’s try with the vectors of the same length first.

Each element of vector x will be compared with each corresponding element in y to check whether the condition satisfies or not. For the values where the condition satisfies, it will give TRUE, and where the condition doesn’t satisfy, it gives False.

Vectors With Different Lengths

Let’s create two vectors having different lengths.

As you can see, it is comparing the first three corresponding elements of both vectors and giving the output “false” for the missing values. So, if we use vectors or matrices of different lengths, it will show a warning message on the console.

## Example # 3: (<=) operator With Matrices in R

Let’s take two matrices as operands to check the relationship between them by using the <= operator. The dimension of both matrices must be similar. Otherwise, the R console will show a warning message.

The <= operator will compare each element in matrix 1 with each corresponding element of matrix m2. As you can see in elements 1 and 4, the condition is satisfied and false at the 2nd and 3rd elements.

## Example # 4: (<=) operator in if-statement and if-else statement in R

The <= can be used in if-statement for creating different applications. Let’s write a program to determine if a number is less than or equal to 11. For this, we will create a numeric variable first. In the if-statement, we will write our condition using the <= operator.

When the value passes to the if-statement, it will check whether the given number is less than or equal to 10 or not. As 5 is less than 10 so, it will print “a is less than 11”.

Let’s try the same above example with the if-else statement.

In the above example, If the condition satisfies, then it will print “a is equal to or a is less than 11” otherwise, it will show an output of “a is greater than 11”. As 12 is greater than 11, so we will get “a is greater than 11”.

## Example # 5: (<=) operator in While Loop in R

The <= operator can also be used for creating a condition inside a while loop. Let’s create a program to print the first five positive integers using a while loop.

The variable value is set to 1 at the start. The condition is verified, and the value of val is printed in each iteration of the while loop, after which it is incremented until it reaches 5 and the condition is false, at which point the loop is ended.

## Conclusion

In this tutorial, we have seen that (the less than and equal to”<=”) is an operator that can be used in conditions to make the comparison between two operands. Now you should be able to use this operator in different applications in the R programming language. We implemented different examples to teach you how to use the <= operator with different data types in R. We have also seen that it can be very useful in if-statement, if-else statements, and while loops in R.