R Programming Tutorial-R Operators

Sharing is Caring
Share

Get Access to
the Machine learning Course!

Are you learning R operators? What about loop, function or visualization with R? Anyway, ultimate goal is to learn machine learning. Right? Don't miss out to this great opportunity to get into machine learning & data science. Enroll today & learn everything about machine learning & data science with R.

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

  1. Arithmetic
  2. Relational
  3. Logical
  4. Assignment
  5. 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


[crayon-5c64f30dd35f5736230591/]

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.

  1. > greater than sign
  2. < less than
  3. == 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.
  4. <= less than or equal to
  5. >= greater than or equal to
  6. != NOT equal to

[crayon-5c64f30dd3615652677098/]

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

[crayon-5c64f30dd3631907948218/]

Assignment Operators in R language

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

  • <-
  • <<-
  • =

R code for assignment operators

[crayon-5c64f30dd3650999305824/]

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.

Sharing is Caring
Share
About akhilendra

Hi, I’m Akhilendra and I write about Product management, Business Analysis, Data Science, IT & Web. Join me on Twitter, Facebook & Linkedin

Speak Your Mind

*