If you want to learn any programming language, you need to start from basics. Basics will include the basic syntax of the language, data types, variables, loops, functions & operators.

In this post, we are going to talk about operators in R programming language. Operators are used one of the pillars of any language and R is not an exception in this case. So you need to pay close attentions to the details here.

## What is an operator in R?

It is a symbol that is used to assign values or run commands in R. Operators are used in all programming languages but there is always some difference between each language. Some of the common examples of operators would include = sign or < > signs.

Let’s dive in R and run some R code to understand the different types of operators in R.

Types of Operators

- Arithmetic
- Relational
- Logical
- Assignment
- Miscellaneous

## Arithmetic Operators in R language

Arithmetic operators are used for running arithmetic, or in simple language, mathematical operations in R language. Let’s look at some of the most common arithmetic operators in R.

- + – plus sign for addition
- – – minus sign for subtraction
- * – multiplication sign for multiplication
- / – division sign for division
- %% – two percent signs, Give the remainder of the first vector with the second
- %/% – percent sign-forward slash-percent sign, The result of division of first vector with second
- ^ first vector raised to the exponent of second vector

R code Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
> 2 + 2 [1] 4 > 3-2 [1] 1 > 2 * 2 [1] 4 > 10 /2 [1] 5 > 11%%2 [1] 1 > 11%/%2 [1] 5 > 2 ^ 3 [1] 8 |

### Relational Operators in R language

Relational operators in R, are usually used when you are looking to compare two values in their relational context. For if you need to determine if something is bigger or smaller than the given value. Their output is usually a Boolean value.

- > greater than sign
- < less than
- == is equal to , please note if you use only = sign, it will be used as assignment operator. If you need to compare two values and determine that they both are equal, you need to pass 2 equal sign for that.
- <= less than or equal to
- >= greater than or equal to
- != NOT equal to

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
> 4 > 5 [1] FALSE > 4 < 5 [1] TRUE > # == is equal to , please only = will be used as assignment operator > 4==5 [1] FALSE > 4==4 [1] TRUE > 4 <= 5 [1] TRUE > 4 >= 5 [1] FALSE > 4 != 5 [1] TRUE |

### Logical Operators in R language

Logical operators are like relational output in their output. They too result in a Boolean value. Relational operators are extension of basic mathematical operations where instead of performing operations like addition, we performed comparison related operations.

Logical operators also conduct comparison-based operations.

- & – Element wise AND operator, gives an output TRUE if both the elements are TRUE or both conditions are passed. In element wise & operator, if you use a vector, all values are compared in both vectors.
- | – Vertical bar or pipe operator, it is an Element wise Logical OR operator. it gives an output TRUE if one of the elements is TRUE or one of the condition passes.
- ! – NOT operator, it can be used with equal sign for “NOT Equal” purpose.

The logical operator && and || considers only the first element of the vectors and give a vector of single element as output.

- && Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE.
- || Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE.

R code examples for logical operators in R

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
> 4 < 5 [1] TRUE > # == is equal to , please only = will be used as assignment operator > 4==5 [1] FALSE > 4==4 [1] TRUE > 4 <= 5 [1] TRUE > 4 >= 5 [1] FALSE > 4 != 5 [1] TRUE > 4 & 5 [1] TRUE > a <- c(TRUE,FALSE,TRUE) > b <- c(TRUE,FALSE,TRUE) > c <- c(FALSE,TRUE,FALSE) > a & b [1] TRUE FALSE TRUE > a & c [1] FALSE FALSE FALSE > # | - Vertical bar or pipe operator, it is an Element wise Logical OR operator. it gives an output TRUE if one of the elements is TRUE or one of the condition passes. > a | b [1] TRUE FALSE TRUE > d <- 4 > e <- 5 # not a very useful if else block, but just for demonstration purpose on how<br /># you can use these operators in ifelse block or even functions > if(d|e){ + print("d or e is equal to given value") + } else { + print("d or e is not equal to given value") + } [1] "d or e is equal to given value" > d != e [1] TRUE > d != 5 [1] TRUE > 4 && 5 [1] TRUE > a && b [1] TRUE > a && c [1] FALSE > a || c [1] TRUE > |

### Assignment Operators in R language

These operators are used to assign values to vectors/data frames/matrix/variables/objects.

- <-
- <<-
- =

R code for assignment operators

1 2 3 |
> f <- 123 > f <<- 123 > f = 123 |

These are some of the most common operators in R programming language. There are few more but they are more specifically used with libraries. Therefore i am not covering them here. If you are looking to learn visualization using ggplot2 in R, please click here. For ANOVA in R, click here.

## Speak Your Mind