0% found this document useful (0 votes)
5 views32 pages

IDS-UNIT-4-FINAL (1)

Uploaded by

pnithishreddy14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views32 pages

IDS-UNIT-4-FINAL (1)

Uploaded by

pnithishreddy14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

INTRODUCTION TO DATA SCIENCE(IDS)

Prepared by N.Pandu Ranga Reddy


UNIT-4

Conditionals and Control Flow in R-


Program
Relational Operators

Operators
Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

R divides the operators in the following groups:

 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:

Operator Name Example Try it

+ Addition x+y Try it »

- Subtraction x-y Try it »


* Multiplication x*y Try it »

/ Division x/y Try it »

^ Exponent x^y Try it »

%% Modulus (Remainder from division) x %% y Try it »

%/% Integer Division x%/%y

R Assignment Operators
Assignment operators are used to assign values to variables:

Assignment operators are <- , <<- , -> , ->>

Example
my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var


R Comparison Operators
Comparison operators are used to compare two values:

Operator Name Example Try it

== Equal x == y Try it »

!= Not equal x != y Try it »

> Greater than x>y Try it »

< Less than x<y Try it »

>= Greater than or equal to x >= y Try it »

<= Less than or equal to x <= y

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

| Elementwise- Logical OR operator. Returns TRUE if one of the statements is TRUE

|| Logical OR operator. Returns TRUE if one of the statements is TRUE

! Logical NOT - Returns FALSE if statement is TRUE

R Miscellaneous Operators
Miscellaneous operators are used to manipulate data:

Operator Description Example

: Creates a series of numbers in a sequence x <- 1:10

%in% Find out if an element belongs to a vector x %in% y

%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

# R program to illustrate
# the use of Arithmetic operators

example 1:

a <- c (1, 0.1)

b <- c (2.33, 4)

print (a+b)

example 2:

vec1 <- c(0, 2)

vec2 <- c(2, 3)

# Performing operations on Operands

cat ("Addition of vectors :", vec1 + vec2, "\n")

cat ("Subtraction of vectors :", vec1 - vec2, "\n")

cat ("Multiplication of vectors :", vec1 * vec2, "\n")

cat ("Division of vectors :", vec1 / vec2, "\n")

cat ("Modulo of vectors :", vec1 %% vec2, "\n")

cat ("Power operator :", vec1 ^ vec2)

example 3:

# R program to illustrate # the use of Logical operators

vec1 <- c(0,2)

vec2 <- c(TRUE,FALSE)

# Performing operations on Operands

cat ("Element wise AND :", vec1 & vec2, "\n")

cat ("Element wise OR :", vec1 | vec2, "\n")

cat ("Logical AND :", vec1[1] && vec2[1], "\n")

cat ("Logical OR :", vec1[1] || vec2[1], "\n")


cat ("Negation :", !vec1)

Output:
Element wise AND : FALSE FALSE
Element wise OR : TRUE TRUE
Logical AND : FALSE
Logical OR : TRUE
Negation : TRUE FALSE

example 4:

list1 <- c(TRUE, 0.1, "apple")

list2 <- c(TRUE, 0.1, "bat")

# Convert lists to character strings

list1_char <- as.character(list1)

list2_char <- as.character(list2)

# Compare character strings

print(list1_char <= list2_char)

Output : TRUE TRUE TRUE


example 5:

val <- 0.1

list1 <- c(TRUE, 0.1,"apple")

print (val %in% list1)

example 6:

# R program to illustrate

# the use of Relational operators

vec1 <- c(0, 2)

vec2 <- c(2, 3)


# Performing operations on Operands

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")

cat ("Vector1 not 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

# R program to illustrate if statement

# assigning value to variable a

a <- 5

# condition
if(a > 0)

print("Positive Number") # Statement

Output:
Positive Number

Example 2: R if statement with optional argument

# Assigning value to variable x

x <- 12

# Condition

if (x > 20)

print("12 is less than 20") # Statement

print("Hello World")

Output:
12 is less than 20
Hello World

Example 3: if…else statement


 R

# R program to illustrate if statement

# assigning value to variable a

a <- -5

# condition

if(a > 0)

print("Positive Number") # Statement

}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".

R uses curly brackets { } to define the scope in the code.

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".

You can use as many else if statements as you want in R.

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".

You can also use else without else if:

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

Loops in R (for, while, repeat)

in R programming, we require a control structure to run a block of code multiple


times.
A loop is a control statement that allows multiple executions of a statement or a set
of statements.
The word ‘looping’ means cycling or iterating.

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.

There are three types of loops in R programming:


 For Loop
 While Loop
 Repeat Loop

R – For loop Syntax:

for (value in sequence)


{
statement
}

# R program to demonstrate the use of for loop

# using for loop

for (val in 1: 5)
{
# statement
print(val)
}

# R program to illustrate
# application of for loop

# assigning strings to the vector


week <- c('Sunday','Monday','Tuesday','Wednesday','Thursday',
'Friday',
'Saturday')

# using for loop to iterate


# over each string in the vector

for (day in week)


{

# displaying each string in the vector


print(day)
}

R For-Loop on a List

 R
# Create a list of numbers

my_list <- list(1, 2, 3, 4, 5)

# Loop through the list and print each element

for (i in seq_along(my_list)) {

current_element <- my_list[[i]]

print(paste("The current element is:", current_element))

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.

R – While loop Syntax:

while ( condition )
{
statement
}
# R program to demonstrate the use of while loop

val = 1

# using while loop

while (val <= 5)


{
# statements

print(val)
val = val + 1
}

# R program to illustrate
# application of while loop

# assigning value to the variable


# whose factorial will be calculated

n <- 5

# assigning the factorial variable


# and iteration variable to 1

factorial <- 1

i <- 1

# using while loop

while (i <= n)
{

# multiplying the factorial variable


# with the iteration variable

factorial = factorial * i

# incrementing the iteration variable

i = i + 1
}

# displaying the factorial

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.

The keyword used for the repeat loop is 'repeat'.

Syntax:
repeat {
commands
if(condition) {
break
}
}

# R program to illustrate repeat loop

result <- c("Hello World")


i <- 1

# 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:

fruits <- list("apple", "banana", "cherry")

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":

fruits <- list("apple", "banana", "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)
}

If .. Else Combined with a For Loop


To demonstrate a practical example, let us say we play a game of Yahtzee!

Example
Print "Yahtzee!" If the dice number is 6:

dice <- 1: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.

Creating a Function in R Programming

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.

Parameters or Arguments in R Functions:

Parameters and arguments are same term in functions.


Parameters or arguments are the values passed into a function.
A function can have any number of arguments, they are separated by comma in
paranthesis.

Example:

# function to add 2 numbers

add_num <- function(a,b)

sum_result <- a+b

return(sum_result)

# calling add_num function

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.

Default Value of Parameter:


Some functions have default values, and you can also give default value in your user-
defined functions. These values are used by functions if user doesn’t pass any parameter
value while calling a function.

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.

Passing Arguments to Functions in R Programming Language

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

# A simple R program to demonstrate

# passing arguments to a function

Rectangle = function(length=5, width=4){


area = length * width

return(area)

# Case 1:

print(Rectangle(2, 3))

# Case 2:

print(Rectangle(width = 8, length = 4))

# Case 3:

print(Rectangle())

Output
[1] 6
[1] 32
[1] 20

Types of Function in R Language

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.

2. User-defined Function: R language allow us to write our own function.

Built-in Function in R Programming Language

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))

# Find max of numbers 4 and 6.

print(max(4:6))

# Find min of numbers 4 and 6.

print(min(4:6))

Output
[1] 15
[1] 6
[1] 4

Other Built-in Functions in R:

Let’s look at the list of built-in R functions and their uses:

Functions Syntax

Mathematical Functions

calculates a number’s absolute


abs() value.

sqrt() calculates a number’s square root.

rounds a number to the nearest


round() integer.
Functions Syntax

calculates a number’s exponential


exp() value

which calculates a number’s natural


log() logarithm.

calculates a number’s cosine, sine,


cos(), sin(), and tan() and tang.

Statistical Functions

A vector’s arithmetic mean is


mean() determined by the mean() function.

A vector’s median value is


median() determined by the median()
function.

calculates the correlation between


cor() two vectors.

calculates the variance of a vector


var() and calculates the standard
deviation of a vector.

Data Manipulation Functions

returns the unique values in a


unique() vector.
Functions Syntax

subsets a data frame based on


subset() conditions.

groups data according to a


aggregate() grouping variable.

uses ascending or descending order


order() to sort a vector.

File Input/Output Functions

read.csv() reads information from a CSV file.

publishes information to write a CSV


Write.csv() file.

Read. table() reads information from a tabular.

Write.table() creates a tabular file with data.

User-defined Functions in R Programming Language

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

A simple R function to check


# whether x is even or odd
# user defined function –evenOdd()
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}

# calling user defined function –


evenOdd()
print(evenOdd(4))
print(evenOdd(3))

Output
[1] "even"
[1] "odd"

R Nested Functions
Nested Functions
There are two ways to create a nested function:

 Call a function within another function.


 Write a function within a function.

Example
Call a function within another function:

Nested_function <- function(x, y) {


a <- x + y
return(a)
}

Nested_function(Nested_function(2,2), Nested_function(3,3))

Try it Yourself »

Example Explained
The function tells x to add y.

The first input Nested_function(2,2) is "x" of the main function.

The second input Nested_function(3,3) is "y" of the main function.

The output is therefore (2+2) + (3+3) = 10.

Example
Write a function within a function:

Outer_func <- function(x) {


Inner_func <- function(y) {
a <- x + y
return(a)
}
return (Inner_func)
}
output <- Outer_func(3) # To call the Outer_func
output(5)

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 call Outer_func first in order to call Inner_func as a second step.

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.

The output is therefore 8 (3 + 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

Why is Lexical Scoping/ static scoping?

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.

Principles of Lexical Scoping

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.

Recursion is a common mathematical and programming concept. It means that a


function calls itself. This has the benefit of meaning that you can loop through data to
reach a result.

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.

In this example, tri_recursion() is a function that we have defined to call itself


("recurse"). We use the k variable as the data, which decrements (-1) every time we recurse.

The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

Example

tri_recursion <- function(k) {


if (k > 0) {
result <- k + tri_recursion(k - 1)
print(result)
} else {
result = 0
return(result)
}
}
tri_recursion(6)

OUTPUT:
[1] 1
[1] 3
[1] 6
[1] 10
[1] 15
[1] 21
Example: Factorial using Recursion in R
 R

rec_fac <- function(x){

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.

Efficient way to install and load R packages


The most common method of installing and loading packages is using
the install.packages() and library() function respectively. Let us see a brief about these
functions –
 Install.packages() is used to install a required package in the R programming
language.

Syntax:
install.packages(“package_name”)

 library() is used to load a specific package in R programming language

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")

Similarly, package can be loaded efficiently by one of the following ways.


Method 1: Using library()
In this, the packages to be loaded are passed to the function but as a list with each
package separated by a comma (,).
Syntax:
library(“package1”, “package2″, . . . . ,”package n”)

Example:
 R

library("ggplot2","dplyr")

You might also like