Professional Documents
Culture Documents
R PRG
R PRG
©©Copyright
Copyright 2015,
2015, Simplilearn.
Simplilearn. All rights
All rights reserved.
reserved.
Objectives
R has many operators to perform different mathematical and logical operations. These can be
categorized as follows:
These operators are used for mathematical operations like addition and multiplication. To understand
their functions better, look at the table below:
Arithmetic Operators in R
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus
%/% Integer Division
These operators are used to compare two values. To understand their functions better, look at the table
below:
Relational Operators in R
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
These operators are used to perform Boolean operations such as “AND” and “OR.” To understand their
functions better, look at the table below:
Logical Operators in R
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
|| Logical OR
These operators are used to assign values to variables. Variables are assigned using “<-”, although “=”
also works.
Examples:
• age <- 18 (left assignment )
• 18 -> age (right assignment)
In if...else statements, when the test expression is True, the code in the “if” block executes; otherwise,
the code in the “else” block executes.
Example:
age <- 20
if(age > 18){
print("Major")
} else {
print(“Minor”)
}
)
In nested if...else statements, only one statement executes, depending on the test expressions in the
“if” blocks.
Example:
x <- 0
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else
print("Zero")
Example:
a = c(1,2,3,4)
ifelse(a %% 2 == 0,"even","odd")
Example 1
Example 2
color = "green«
switch(color, "red"={print("apple")}, "yellow"={print("banana")}, "green"={print("avocado")})
For Loop
While Loop
Repeat Loop
For Loop
Example:
While Loop vec <- c(1,2,3,4,5)
for (val in vec) {
print(val)
Repeat Loop }
In the while loop, while the test expression remains True, the code inside the loop keeps on executing.
A repeat loop iterates a code multiple times. Since there is no conditional check to exit the loop, you
must specify it inside the body of the loop.
Example:
For Loop x <- 1
repeat {
While Loop print(x)
x = x+1
if (x == 6){
Repeat Loop break
}
}
When present inside a loop, it stops the iterations from executing and forces the flow to jump off the
loop.
Example:
num <- 1:5
for (val in num) {
if (val == 3){
break
}
print(val)
}
Example:
num <- 1:5
for (val in num) {
if (val == 3){
next
}
print(val)
}
Example:
From the Console
x <- scan()
Example:
From a File x <- scan("http://www.ats.ucla.edu/stat/data/scan.txt", what = list(age = 0,name =
""))
! Using this function may not always be an efficient way of reading files.
Example:
source("myScript.R", echo=TRUE)
Examples:
• R CMD BATCH my_script.R
• $ Rscript myScript.R arg1 arg2
The table below lists and explains the commonly used R functions:
Function Description
append() Add elements to a vector
c() Combine values into a vector or list
identical() Test if two objects are exactly equal
length() Return the length of the R object
ls() List objects in the current environment
range(x) Return the minimum and maximum of the vector
rep(x,n) Repeat the number x, n times
rev(x) Provide the reversed version of its argument
seq(x,y,n) Generate regular sequences from x to y, spaced by n
unique(x) Remove duplicate entries from the vector
Function Description
tolower() Convert a string to lower case letters
toupper() Convert a string to upper case letters
grep() Use for regular expressions
summary(x) Return object summaries
str(x) Compactly display the structure of an arbitrary R object
glimpse(x) Compactly display the structure of an arbitrary R object
(dplyr package)
class(x) Return the class of an object
mode(x) Get or set the type or storage mode of an object
summary(x) Return object summaries
a. /
b. *
c. %/%
a. /
b. *
c. %/%
a. 18
b. Error
c. NA
d. Binary value of 18
a. 18
b. Error
c. NA
d. Binary value of 18
Explanation: The output of “age <- 18, 18 -> age, print(age)” will be 18.
a. Yes
b. No
c.
d.
a. Yes
b. No
c.
d.
Explanation: There will be no output of the given command; however, the program will run.
Let us summarize the • Four types of operators in R are arithmetic, relational, logical, and assignment.
topics covered in this • Two types of conditional statements in R are if…else and nested if…else.
lesson:
• Three types of loops in R are for loop, while loop, and repeat loop.
• To run an R script, use the “source” function.
• To run a batch script in R, use the “R CMD BATCH” command.
• The commonly used functions in R are given below:
append() unique(x)
c() tolower()
identical() toupper()
length() grep()
ls() summary(x)
range(x) str(x)
rep(x,n) glimpse(x)
rev(x) class(x)
seq(x,y,n) mode(x)
© Copyright 2015, Simplilearn. All rights reserved.
This concludes “R Programming.”
The next lesson is “R Data Structure.”