0% found this document useful (0 votes)
2 views

Statistics and Data Science with R Part -2

Uploaded by

Mahima Mehra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Statistics and Data Science with R Part -2

Uploaded by

Mahima Mehra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Statistics

and
Data Science with R
Example:
Switch Statement
In R, the switch statement is a type of conditional day <- 3
control flow that selects one of several code blocks to
execute based on the value of a specified expression. weekday <- switch(
It's particularly useful when you have multiple cases to
evaluate against a single expression. day,
"Monday",
Syntax: "Tuesday",
switch(EXPR, CASE1, CASE2, ..., CASEn, default) "Wednesday",
"Thursday",
"Friday",
"Weekend"
)
print(weekday)
Loop

Loops are used to repeatedly execute a block of code until a specified condition is met.

Loop

while loop for loop repeat loop


while loop
Example:
A while loop is used to repeatedly execute a block of
code as long as a specified condition is TRUE. count <- 1
while (count <= 5) {
Syntax: print(paste("Count:", count))
while (condition) count <- count + 1
{ }
# Code block to be executed as long as the condition
is TRUE
}
for loop
Example:
A for loop is used to iterate over a sequence (such as
a vector, list, or sequence of numbers) and perform
for (i in 1:5)
operations for each element in the sequence. These
loops are used when you know in advance how many {
times you want to execute a block of code. print(paste("Iteration:", i))
}
Syntax:
for (variable in sequence) {
# Code block to be executed for each element in the
sequence
}
repeat loop
Example:
A repeat loop executes a block of code repeatedly until
an explicit break statement is encountered to exit the
loop. x <- 1
repeat {
Syntax: print(x)
repeat { x <- x + 1
# Code block to be executed repeatedly
if (condition) { if (x > 5) {
break # Breaks the loop based on a condition break
} }
} }
next Statement
The next statement is used to skip the current iteration Example:
of a loop and move on to the next iteration without
executing the remaining code within the loop for the
for (i in 1:5) {
current iteration.
if (i == 3) {
next # Skips the iteration when i is equal to 3
}
print(i)
}
break Statement
Example:
The break statement is used to terminate the execution
of a loop prematurely when a specific condition is met. It
i <- 1
exits the loop entirely, regardless of whether its condition
is true. while (i <= 10) {
if (i == 5) {
break # Terminates the loop when i is
equal to 5
}
print(i)
i <- i + 1
}
Functions

In R, functions are blocks of reusable code designed to perform a specific task. They allow you to encapsulate a
set of operations into a single unit that can be called multiple times with different inputs.

Functions

User-defined
Built-in functions
functions
User Define Function
User-defined functions in R allow you to create your # Function to calculate the square of a
own functions to perform specific tasks or operations. number
These functions encapsulate a set of operations or square <- function(x) {
algorithms you can reuse multiple times with different
return(x^2)
inputs. Creating user-defined functions helps
modularize code, enhance readability, and promote }
code reusability.
# Calling the function with an argument
Syntax: result <- square(5)
function_name <- function(arg1, arg2, ...) print(result)
{
# Code block specifying the operations to be
performed
# Use 'return()' to specify the output of the function
return(output)
}
Scope of Variables # Global variable
global_var <- "I am global"
Variables in R can have different scopes:
my_function <- function() {
• Global Scope: Variables defined in the global # Local variable
environment.
local_var <- "I am local"
• Local Scope: Variables defined inside functions or
print(local_var)
loops.
}

my_function()
print(global_var)
print(local_var) # Error: object
'local_var' not found

© 2024 Grant Thornton International Ltd |


R Packages

In R, packages are collections of functions, data, and


compiled code that extend the capabilities of R. They
provide additional functionalities beyond the base R
installation.

Benefits of Using R Packages:


•Save Time and Effort
•Specialized Tools
•Standardized Code
•Active Community
Installation of R Package
Example:
# Installing the ggplot2 package
Syntax: install.packages("ggplot2")

# Installing a package from CRAN


install.packages("package_name")

© 2024 Grant Thornton International Ltd |


We are
Shaping Vibrant Bharat
A member of Grant Thornton International Ltd, Grant Thornton Bharat is at the forefront of helping reshape the values in
the profession. We are helping shape various industry ecosystems through our work across Assurance, Tax, Risk,
Transactions, Technology and Consulting, and are going beyond to shape more #VibrantBharat.

Our offices in India


Ahmedabad Bengaluru Chandigarh Chennai Dehradun Scan QR code to see our office addresses
Goa Gurugram Hyderabad Kochi Kolkata Mumbai www.grantthornton.in
New Delhi Noida Pune

Connect
@Grant-Thornton-Bharat-LLP @GrantThorntonBharat @Grantthornton_bharat @GrantThorntonIN @GrantThorntonBharatLLP [email protected]
with us

© 2024 Grant Thornton Bharat LLP. All rights reserved.


“Grant Thornton Bharat” means Grant Thornton Advisory Private Limited, a member firm of Grant Thornton International Limited (UK) in India, and those legal entities which are its related parties as defined by the Companies Act, 2013,
including Grant Thornton Bharat LLP.
Grant Thornton Bharat LLP, formerly Grant Thornton India LLP, is registered with limited liability with identity number AAA-7677 and has its registered office at L-41 Connaught Circus, New Delhi, 110001.
References to Grant Thornton are to Grant Thornton International Ltd. (Grant Thornton International) or its member firms. Grant Thornton International and the member firms are not a worldwide partnership. Services are delivered
independently by the member firms.

You might also like