R Operators

Spread the love

In this post, you will learn about various operators in R programming such as Arithmetic, Logical, Comparison, Assignment and Miscellaneous Operators.

Operators –

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values

> 10 + 5
[1] 15

Types of Operators in R language –

There are various types of operators in R language –

  1. Arithmetic Operators
  2. Logical Operators
  3. Comparison Operator
  4. Assignment Operators
  5. Miscellaneous Operators

1 . Arithmetic Operators –

Arithmetic operators are used with numeric values to perform common mathematical operations.

+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
^Exponentx ^ y
%%Modulus (Remainder from division)x %% y
%/%Integer Division

2 . Logical Operators –

The logical operators allow a program to make a decision on the basis of multiple conditions. In the program, each operand is considered as a condition which can be evaluated to a false or true value. The value of the conditions is used to determine the overall value of the op1 operator op2.

The logical operator compares each element of the first vector with the corresponding element of the second vector.

&Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&&Logical AND operator – Returns TRUE if both statements are TRUE
|Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
||Logical OR operator. It returns TRUE if one of the statement is TRUE.
!Logical NOT – returns FALSE if statement is TRUE

3. Comparison Operators –

Comparison operators are used to compare two values.

==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

4. Assignment Operators –

An assignment operator is used to assign a new value to a variable. In R, these operators are used to assign values to vectors. There are the following types of assignment.

<- or = or <<-These operators are known as left assignment operatorsa <- c(3, 0, TRUE, 2+2i)
b <<- c(2, 4, TRUE, 2+3i)
d = c(1, 2, TRUE, 2+3i)
-> or ->>These operators are known as right assignment operators.c(3, 0, TRUE, 2+2i) -> a
c(2, 4, TRUE, 2+3i) ->> b

5. Miscellaneous Operator –

Miscellaneous operators are used to manipulate data.

:Creates a series of numbers in a sequencex <- 1:10
%in%Find out if an element belongs to a vectorx %in% y
%*%Matrix Multiplicationx <- Matrix1 %*% Matrix2

Rating: 1 out of 5.

Posted in RTagged

Leave a Reply