IDS-UNIT-4-FINAL (1)
IDS-UNIT-4-FINAL (1)
Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Miscellaneous operators
R Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
R Assignment Operators
Assignment operators are used to assign values to variables:
Example
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var
== Equal x == y Try it »
R Logical Operators
Logical operators are used to combine conditional statements:
Operator Description
& Element-wise Logical AND operator. Returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
R Miscellaneous Operators
Miscellaneous operators are used to manipulate data:
# R program to illustrate
# the use of Arithmetic operators
example 1:
b <- c (2.33, 4)
print (a+b)
example 2:
example 3:
Output:
Element wise AND : FALSE FALSE
Element wise OR : TRUE TRUE
Logical AND : FALSE
Logical OR : TRUE
Negation : TRUE FALSE
example 4:
example 6:
# R program to illustrate
cat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")
cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")
cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")
cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")
Conditional Statements.
If statement is one of the Decision-making statements in the R programming language. It
is one of the easiest decision-making statements. It is used to decide whether a certain
statement or block of statements will be executed or not i.e if a certain condition is true
then a block of statement is executed otherwise not.
Syntax:
if (expression) {
#statement to execute if condition is true
}
If the expression is true, the statement gets executed. But if the expression is FALSE,
nothing happens. The expression can be a logical/numerical vector, but only the first
element is taken into consideration. In the case of numeric vector, zero is taken as
FALSE, rest as TRUE.
Example of if statement in R
Example 1: R if statement
a <- 5
# condition
if(a > 0)
Output:
Positive Number
x <- 12
# Condition
if (x > 20)
print("Hello World")
Output:
12 is less than 20
Hello World
a <- -5
# condition
if(a > 0)
}else{
print("-ve number")
Output:
"-ve number"
The if Statement
An "if statement" is written with the if keyword, and it is used to specify a block of code to be
executed if a condition is TRUE:
Example
a <- 33
b <- 200
if (b > a) {
print("b is greater than a")
}
Try it Yourself »
In this example we use two variables, a and b, which are used as a part of the if statement to test
whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so
we print to screen that "b is greater than a".
Else If
The else if keyword is R's way of saying "if the previous conditions were not true, then try this
condition":
Example
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print ("a and b are equal")
}
Try it Yourself »
In this example a is equal to b, so the first condition is not true, but the else if condition is
true, so we print to screen that "a and b are equal".
If Else
The else keyword catches anything which isn't caught by the preceding conditions:
Example
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print("a and b are equal")
} else {
print("a is greater than b")
}
Try it Yourself »
In this example, a is greater than b, so the first condition is not true, also the else if condition
is not true, so we go to the else condition and print to screen that "a is greater than b".
Example
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else {
print("b is not greater than a")
}
Iterative Programming in R
A loop asks a query, in the loop structure. If the answer to that query requires an
action, it will be executed. The same query is asked again and again until further
action is taken.
Any time the query is asked in the loop, it is known as an iteration of the loop.
There are two components of a loop, the control statement, and the loop body.
The control statement controls the execution of statements depending on the
condition and the loop body consists of the set of statements to be executed.
In order to execute identical lines of code numerous times in a program, a
programmer can simply use a loop.
for (val in 1: 5)
{
# statement
print(val)
}
# R program to illustrate
# application of for loop
R For-Loop on a List
R
# Create a list of numbers
for (i in seq_along(my_list)) {
We have a list of five numbers in this case. The seq_along() function is used to
create a list of indices to loop through, and double brackets [[]] are used to retrieve
the current element during each loop iteration. We output a message showing the
element we’re dealing with inside the loop, followed by the value of that element.
While Loop in R
It is a type of control statement that will run a statement or a set of statements
repeatedly unless the given condition becomes false.
It is also an entry-controlled loop, in this loop, the test condition is tested first,
then the body of the loop is executed, the loop body would not be executed if the
test condition is false.
while ( condition )
{
statement
}
# R program to demonstrate the use of while loop
val = 1
print(val)
val = val + 1
}
# R program to illustrate
# application of while loop
n <- 5
factorial <- 1
i <- 1
while (i <= n)
{
factorial = factorial * i
i = i + 1
}
print(factorial)
Repeat loop
in R is used to iterate over a block of code multiple number of times. And also it
executes the same code again and again until a break statement is found.
Repeat loop, unlike other loops, doesn’t use a condition to exit the loop instead it
looks for a break statement that executes if a condition within the loop body results
to be true.
An infinite loop in R can be created very easily with the help of the Repeat loop.
Syntax:
repeat {
commands
if(condition) {
break
}
}
# test expression
repeat {
print(result)
# update expression
i <- i + 1
# Breaking condition
if(i >5) {
break
}
}
A for loop is used for iterating over a sequence:
Example
for (x in 1:10) {
print(x)
}
Try it Yourself »
Example
Print every item in a list:
for (x in fruits) {
print(x)
}
Break
With the break statement, we can stop the loop before it has looped through all the
items:
Example
Stop the loop at "cherry":
for (x in fruits) {
if (x == "cherry") {
break
}
print(x)
}
Next
With the next statement, we can skip an iteration without terminating the loop:
Example
Skip "banana":
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
if (x == "banana") {
next
}
print(x)
}
Example
Print "Yahtzee!" If the dice number is 6:
for(x in dice) {
if (x == 6) {
print(paste("The dice number is", x, "Yahtzee!"))
} else {
print(paste("The dice number is", x, "Not Yahtzee"))
}
}
Functions in R:
A function accepts input arguments and produces the output by executing valid R
commands that are inside the function.
Functions are useful when you want to perform a certain task multiple times.
In R Programming Language when you are creating a function the function name and the
file in which you are creating the function need not be the same and you can have one or
more functions in R.
Functions are created in R by using the command function(). The general structure of the
function file is as follows:
Note: In the above syntax f is the function name, this means that you are creating a
function with name f which takes certain arguments and executes the following
statements.
Example:
return(sum_result)
sum = add_num(35,34)
#printing result
print(sum)
No. of Parameters:
Function should be called with right no. of parameters, neither less nor more or else it will
give error.
Return Value:
You can use return() function if you want your function to return the result.
Calling a Function in R
After creating a Function, you have to call the function to use it.
Calling a function in R is very easy, you can call a function by writing it’s name and
passing possible parameters value.
There are several ways you can pass the arguments to the function:
Case 1: Generally in R, the arguments are passed to the function in the same order as
in the function definition.
Case 2: If you do not want to follow any order what you can do is you can pass the
arguments using the names of the arguments in any order.
Case 3: If the arguments are not passed the default values are used to execute the
function.
Now, let us see the examples for each of these cases in the following R code:
R
return(area)
# Case 1:
print(Rectangle(2, 3))
# Case 2:
# Case 3:
print(Rectangle())
Output
[1] 6
[1] 32
[1] 20
1. Built-in Function: Built-in functions in R are pre-defined functions that are available
in R programming languages to perform common tasks or operations.
Built-in Function are the functions that are already existing in R language and you just
need to call them to use.
Here we will use built-in functions like sum(), max() and min().
# Find sum of numbers 4 to 6.
print(sum(4:6))
print(max(4:6))
print(min(4:6))
Output
[1] 15
[1] 6
[1] 4
Functions Syntax
Mathematical Functions
Statistical Functions
User-defined functions are the functions that are created by the user.
User defines the working, parameters, default parameter, etc. of that user-defined
function. They can be only used in that specific code.
Example
Output
[1] "even"
[1] "odd"
R Nested Functions
Nested Functions
There are two ways to create a nested function:
Example
Call a function within another function:
Nested_function(Nested_function(2,2), Nested_function(3,3))
Try it Yourself »
Example Explained
The function tells x to add y.
Example
Write a function within a function:
Try it Yourself »
Example Explained
You cannot directly call the function because the Inner_func has been defined (nested) inside the
Outer_func.
We need to create a new variable called output and give it a value, which is 3 here.
We then print the output with the desired value of "y", which in this case is 5.
Function Scoping
A scope defines the value of variables located in some particular location of our source code.
They are used so that we can use the same variable names in our code repeatedly.
Lexical Scoping is a set of rules which provides us with how the values of some variables will be taken
from an environment.
Scope of Variables in R
The scope of a variable is the location where we can find and access a variable if required. A scope is
used to keep the variable having the same name confined to separate parts of the program. In R, there
are mainly two types of variable scopes.
Global Scope: In the global scope, the variables can be accessed and changed from any part of the
code. All the variables in the global scope exist throughout the execution of a program.
Local Scope: In the local scope, the variables exist only in a certain part of the program, like a
function. Whenever a function call ends, the memory for these variables is released as w
A lexical scoping is the set of rules that are imposed on the program's variables and functions. It is also
known as static scoping.
Via lexical scoping, a function is able to access variables from the parent to the global scope.
Thus a child function is lexically bound to the parent function. While using lexical scoping, we
only need to look at the function definition and not the function nesting to know the
value of a variable.
There are four basic principles behind R’s implementation of lexical scoping:
1. Name Masking
2. Functions vs variables
3. A fresh start
4. Dynamic Lookup
Let’s discuss each principle one by one.
Name Masking
The following example illustrates the most basic principle of lexical scoping, and you
should have no problem predicting the output.
If variable is not defined inside the function:
Example:
c <- 10
f <- function(a, b)
a +b +c
}
f(8, 5)
Output:
[1] 23
It takes the c value as 10 and then adds these numbers and finally we are having 23 as
output.
If name is not defined inside the function:
If a name isn’t defined inside a function, R will look one level up.
Example:
a <- 10
b <- function()
c <- 11
c(a, c)
b()
Output:
[1] 10 11
When one function is defined inside another function:
The same rules apply if a function is defined inside another function: look inside the
current function, then where that function was defined, and so on, all the way up to the
global environment, and then on to other loaded packages.
Example:
a <- 10
g <- function(){
b <- 20
h <- function(){
c <- 30
c(a, b, c)
h()
g()
Output:
[1] 10 20 30
When functions are created by another function:
The same rules apply to closures, functions created by other functions.
Example:
a <- function(z){
b <- 10
function(){
z +4*b
x <- a(10)
x()
Output:
[1] 50
R returns the accurate value of b after calling the function because x preserves the
environment in which it was defined. The environment includes the value of b.
R Function Recursion
Recursion
R also accepts function recursion, which means a defined function can call
itself.
The developer should be very careful with recursion as it can be quite easy to slip into writing a
function which never terminates, or one that uses excess amounts of memory or processor
power.
However, when written correctly, recursion can be a very efficient and mathematically-elegant
approach to programming.
The recursion ends when the condition is not greater than 0 (i.e. when it is 0).
Example
OUTPUT:
[1] 1
[1] 3
[1] 6
[1] 10
[1] 15
[1] 21
Example: Factorial using Recursion in R
R
if(x==0 || x==1)
return(1)
else
return(x*rec_fac(x-1))
rec_fac(5)
Output:
[1] 120
Here, rec_fac(5) calls rec_fac(4), which then calls rec_fac(3), and so on until the input
argument x, has reached 1. The function returns 1 and is destroyed. The return value is
multiplied by the argument value and returned. This process continues until the first
function call returns its output, giving us the final result.
Syntax:
install.packages(“package_name”)
Syntax:
library(package_name)
In the case where multiple packages have to installed and loaded these commands have
to be specified repetitively. Thus making the approach inefficient.
Example:
R
install.packages("ggplot2")
install.packages("dplyr")
install.packages("readxl")
library(ggplot2)
library(dplyr)
library(readxl)
The most efficient way to install the R packages is by installing multiple packages at a
time using. For installing multiple packages we need to use install.packages( ) function
again but this time we can pass the packages to be installed as a vector or a list with
each package separated by comma(,).
Syntax :
install.packages ( c(“package 1″ ,”package 2”, . . . . , “package n”) )
install.packages(“package1″,”package2”, . . . . , “package n”)
Example :
R
install.packages(c("ggplot2","dplyr","readxl"))
install.packages("ggplot2","dplyr","readxl")
Example:
R
library("ggplot2","dplyr")