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

R Language Quiz 15: Important MCQs

The post is about R Language Quiz with Answers. The quiz covers, MCQs about Data Structure, Data Analysis in R, and Some Basics of R Programming Languages. Let us start with the R Programming MCQs Quiz.

Multiple Choice Questions about R Language with Answers

1. R language has a superficial similarity with ———.

 
 
 
 

2. Which of the following is not an R object?

 
 
 
 

3. Which of the following can be used to display the names of (most of) the objects that are currently stored within R?

 
 
 
 

4. Matrices can be created by row-binding using the function

 
 
 
 

5. Which of them is not a basic datatype in R?

 
 
 
 

6. What is the function to give names to columns for a matrix?

 
 
 
 

7. What is the class of the object $y$

y <- c(FALSE, 2)

 
 
 
 

8. What is the output of the following

d <- diag(5, nrow = 2, ncol = 2); d

 
 
 
 

9. What is NaN called?

 
 
 
 

10. R files have an extension ———-.

 
 
 
 

11. A ——– is a variable that holds one value at a time

 
 
 
 

12. What function is used to test objects if they are NaN?

 
 
 
 

13. How many atomic vector types does R have

 
 
 
 

14. what is the output of the following

y <- c(TRUE, 2)

 
 
 
 

15. Dataframes can be converted into a matrix by calling the following function data ———-

 
 
 
 

16. The dimension attribute is itself an integer vector having length ——-.

 
 
 
 

17. what is the class of object $y$

y <- c(2, "t")

 
 
 
 

18. How one can create an integer say 5?

 
 
 
 

19. What is the meaning of “<-” in R

 
 
 
 

20. Which of the following is an alternative to ‘?’ symbol ———.

 
 
 
 

R Language Quiz with Answers

  • What is NaN called?
  • what is the output of the following y <- c(TRUE, 2)
  • what is the class of object $y$ y <- c(2, “t”)
  • What is the class of the object $y$ y <- c(FALSE, 2)
  • Which of them is not a basic datatype in R?
  • How one can create an integer say 5?
  • The dimension attribute is itself an integer vector having length ——-.
  • Matrices can be created by row-binding using the function
  • What function is used to test objects if they are NaN?
  • What is the function to give names to columns for a matrix?
  • How many atomic vector types does R have
  • What is the output of the following d <- diag(5, nrow = 2, ncol = 2); d
  • Which of the following can be used to display the names of (most of) the objects that are currently stored within R?
  • A ——– is a variable that holds one value at a time
  • What is the meaning of “<-” in R
  • Which of the following is not an R object?
  • Dataframes can be converted into a matrix by calling the following function data ———-
  • R files have an extension ———-.
  • R language has a superficial similarity with ———.
  • Which of the following is an alternative to ‘?’ symbol ———.
R Language Quiz with Answers

https://itfeature.com

https://gmstat.com

Read Data from CSV File

Introduction to Read Data From CSV File

In R Language one can easily read data from CSV file format. One can use the read.csv() function. There are different ways to read the CSV file in R and the read.csv() function has many useful arguments.

It is important to note that a CSV file is a comma-separated value file. Usually, CSV files are generated from spreadsheet-like software such as MS Excel. Regarding the file type CSV files are very similar to txt files, however, CSV files can be easily opened in MS Excel. The read.csv() function imports the CSV file as a data frame in R Language, a fundamental data structure in R.

read.csv Function in R

Using the read.csv function in R one can read the data from a CSV file by choosing the file (a dialog box opens to select the appropriate file). This is the easy way to choose a data file as the user does not need to type the file path. For example,

data <- read.csv(file.choose(),header =TRUE)

The file.choose() argument will open a dialog box for the selection of the required file.

Read Data from CSV File

After selecting the data file, one can use the data and may display and get the data information, such as

head(data)
str(data)

There is another way to read the data by giving the complete path to the file with the data file name and its extension. The read.csv function in R can be used with important arguments, such as file path and header=TRUE.

data <- read.csv("C:\\book1.csv", header=TRUE)
data <- read.csv("C:\\mywork\\data\\book1.csv", header=TRUE)

After reading the data file, one can check the names of each variable by using names() function.

names(data)

Selecting Variables from Data Object

One can select a column (variable) by using square brackets and column index or by use of a dollar sign. For example

data$X1    # Selects the variable X1
data[, 1]  # selects the variable in column 1
data[, 4]  # selects the variable in column 4
data[, 1:3] # selects column 1, 2 and 3 

Similarly, one can also select the rows from a data file. For example

data[12, ]   # select the 12 observation/ row of all variables (columns)
data[5:10, ] # selects rows 5 to 10 with all columns/variables

One can also subset the data by using some conditional operator. For example, the following command reads $X_1$ variable from data having greater than 0.7 values.

data1[data1$X1 > 0.7, ]

Read a CSV File as a Table

One can also read a CSV file as a table. For example,

data <- read.table("C:\\data.csv",sep ",",header True)

Some important arguments related to read.csv() function:

  • file: The file argument is used to specify the path to the CSV file. One can provide either the absolute path (e.g., “C:/Users/yourname/Documents/data.csv”) or the relative path if the file is in the working directory.
  • header (optional): The header argument is logical (either TRUE or FALSE), it indicates whether the first row of the CSV file contains names of the columns. By default, header=TRUE. In case, if the file does not have a header row, set it header=FALSE.
  • sep (optional): The sep argument specifies the delimiter (separator) used between values in the CSV file. The default is a comma (“,”).
  • dec (optional): The dec argument defines the decimal point character used in the CSV file. The default is “.”.
RFAQS.com Read Data From CSV File

https://itfeature.com

https://gmstat.com