Operators in R Language Made Easy

Introduction to Operators in R Language

In R language, different types of operators (symbols) are used to perform mathematical and logical computations. R Language is enriched with built-in operators.

Operators in R

The types of operators in the R language are:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators
Operators in R Language

Arithmetic Operators in R

The arithmetic operators in R can be used to perform basic mathematical computations (such as addition, subtraction, multiplication, and division) on numbers or elements of the vectors. The following are some examples, related to arithmetic operators.

# Add two vectors
v1 <- c(3,4,5,6)
v2 <- 1:4
print(v1+v2)

# Subtract 2nd vector from 1st
v2 - v1

# Multiply both vectors
v1 * v2

# Divide the 1st vector witht the 2nd
v1/v2

# Compute the remainder by dividing 1st vector with 2nd
v1%%v2

# Compute the Quotient by division of 1st vector with the second
v1%/%v2
# Compute raised to power of other vecotor
v1^v2
Arithmetic Operators in R Language

Relational Operators in R

The relational operators are used for comparison purposes. When comparing elements of two vectors, each element of the first vector is compared with the corresponding element of the second vector and results in a Boolean value. The examples are:

# less than comparison
v1 < v2

# greater than comparison
v1 > v2

# exactly equal comparison
v1 == v2

# less than or equal to comparison
v1 <= v2

# greater than or equal to comparison
v1 >= v2

# not equal to comparison
v1 != v2
Relational Operators in R Language

Logical Operators in R

The logical operators are used to compare vectors having types of logical (TRUE or FALSE), numeric, or complex numbers. The vectors having values greater than 1 are all considered logical TRUE values.

The examples that make use of logical operators are:

L1 <- c(2, TRUE, 2+2i, FALSE)
L2 <- c(4, 1, 3+1i, TRUE)
# logical AND Operator (Results in TRUE if corresponding elements of vectors are TRUE only)
L1 & L2

# logical OR Operator (Results in TRUE, if either corresponding element of a vector is TRUE
L1 | L2

# logical NOT Operator (Results in opposite logical value)
!L1
Logical Operator in R Language

The logical operators && and || consider the first element of the vectors and give a vector of a single element as output. The && (AND) operator takes the first element of both the vectors and gives the TRUE only if both elements are TRUE. The || (OR) operator takes the first element of both vectors and gives the TRUE if one of them is TRUE.

# && Operator
L1 && L2

# || Operator
L1 || L2

Assignment Operators in R

The assignment operators are used to assign values to vectors or variables. The examples are

# <-, =, and <<- assignment operator (Left Assignment)
x1 <- c(3, 5, 6, 7, 8, 9)
x2 =  c(3, 5, 6, 7, 8, 9)
x3 <<-c(3, 5, 6, 7, 8, 9)

# ->, --> (Right Assignment)
x4 -> c(3, 5, 6, 7, 8, 9)
x5 ->>c(3, 5, 6, 7, 8, 9)

Miscellaneous Operators in R

These operators are used for specific purposes and are not general mathematical or logical computers. These operators include the colon operator, %in% operator, and %*% operator. The Colon operator generates the series of numbers in sequence for a vector. The %in% identifies an element that belongs to a vector and multiplies a matrix with its transpose, matrix multiplication.

# Colon (:) Operator
2:10

# %in% Operator
v1 <- c(5, 6, 4, 7, 8, 9, 2, 3, 4)
4 %in% v1

# %*% Operator
M = matrix(c(3,5,6, 3,2,4), nrow = 2, ncol= 3)
m%*%t(M)

https://itfeature.com

https://rfaqs.com

How to Round Off Numbers in R: A Comprehensive Guide

The R language is capable of performing from easy to advanced numerical calculations. Although R can compute any computation up to 16 digits accurately, a user may not always want to use (or get) that too many digits in his final results or computations. In such cases, one can use a couple of functions to round off numbers in R Language. To round off a number to two or more digits after the decimal point, one can use the round() function as follows:

Round off Numbers in R Language

round(123.456,digits = 2)

##
123.46

One can also use the round() function to round off numbers to multiples of 10, 100, and so on. For that purpose, one just needs to add a negative number as the digits argument: For example

round(-123.456,digits = -2)

##
-100
Round of Numbers in R Language

Significant Digits in R Language

If someone needs to specify the number of significant digits to be retained, regardless of the size of the number, you use the signif() function instead:

signif(-123.456,digits = 4)
##
-123.5

signif(-123.456, digits=3)
##
-123

signif(-123.456, digits=2)
##
-120

Both round() and signif() round off the numbers to the nearest possible number. So, if the first digit that is dropped is smaller than 5, the number is rounded down. If the number is bigger than 5, the number is rounded up. On the other hand, if the first digit that is dropped is exactly 5, R Language uses a rule that is common in programming languages: Always round to the nearest even number. For example, round(1.5) and round(2.5) both return 2, Similarly, for example, round(-4.5) returns -4.

Rounding off Numbers floor(), ceiling(), and trunc() Functions

Contrary to round(), three other functions always round off the numbers in the same direction:

floor(x) rounds to the nearest integer that is smaller than $x$. So, floor(123.45) becomes 123 and floor(-123.45) becomes –124.

ceiling(x) rounds to the nearest integer that’s larger than $x$. This means ceiling(123.45) becomes 124 and ceiling(-123.45) becomes –123.

trunc(x) rounds to the nearest integer in the direction of 0. So, trunc(123.65) becomes 123 and trunc(-123.65) becomes –123.

https://itfeature.com

https://gmstat.com

R Language Basics and R FAQs: Learning Made Easy

The post is about R Language Basics and R Language Frequently Asked Questions. The contents are in the form of questions and answers. Let us start with R Language Basics Questions and Answers.

R Basics

Question: How to start (Run) R Language in the Windows Operating System?
Answer: In Microsoft Windows, during installation, the R installer will have created a Start menu item and an icon for R on your system’s desktop. Double-click the R icon from the desktop or the start menu list to Run the R program.

For Windows 7, 8, or 10, you can use a search term like “R x64 3.2.1” (64-bit version) or “R i386 3.2.1” (32-bit version). R GUI will launch.

Using R as a Calculator

Question: How R can be used as a calculator?
Answer: One can easily use R as a calculator. Starting R will open the console where the user can type commands. To use R as a calculator one has to enter the arithmetical expression after > prompt. For example

5 + 4
sqrt(37)
2*4^2+17*4-3
R language Basics and R as Calculator

R Workspace

Question: What is a workspace in R?
Answer: The workspace in R is an image that contains a record of the computations one has done and it may contain some saved results.

Question: How to record works[ace in R?
Answer: Rather than saving the workspace, one can record all the commands that one has entered in the R console. Recording work in R, the R workspace can be reproduced. The easiest way is to enter the commands in R’s script editor available in the File menu of R GUI.

R Script Editor

Question: What is R Script Editor?
Answer: The r script editor is a place where one can enter commands. Commands can be executed by highlighting them and hitting CTRL+R (mean RUN). At the end of an R session, one can save the final script for a permanent record of one’s work.

A text editor such as Notepad can also be used for this purpose.
Note that in the R console, only one command can be entered at a time because after pressing the Enter key the R command executed immediately.

Quitting R Session

Question: How to quit the R session?
Answer: In the R console on the R command prompt just type

q( )

Question: What is q()?
Answer: The q() is a function that is used to tell R to quit. When q() is entered in the R console and press the Enter key, you will be asked whether to save an image of the current workspace or not or to cancel. Note that only typing q tells R to show the content of this function. The action of this function is to quit R.

Frequently Asked Questions About R

MCQs Statistics with Answers

Computer MCQs Online Test