0% found this document useful (0 votes)
10 views42 pages

R Programming: Flow Control & Data Structures

The document provides an overview of R programming focusing on flow control and data structures, including break and next statements, repeat loops, vectors, lists, and matrices. It explains how to use these elements in R, including syntax, examples, and methods for accessing and modifying data. Additionally, it covers the importance of using functions like c(), length(), and %in% for managing data structures effectively.

Uploaded by

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

R Programming: Flow Control & Data Structures

The document provides an overview of R programming focusing on flow control and data structures, including break and next statements, repeat loops, vectors, lists, and matrices. It explains how to use these elements in R, including syntax, examples, and methods for accessing and modifying data. Additionally, it covers the importance of using functions like c(), length(), and %in% for managing data structures effectively.

Uploaded by

vk3573373
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SIP-I on

R Programming
By
Dr. Devanshu Tiwari
Assistant Professor
Centre for Computer Science and Business Management
MITS-DU, Gwalior

12/12/2024
Day 4 contents
R Flow control and Data Structures
• R break and next
• R repeat Loop
• R Vectors
• R Lists
• R Matrices
12/12/2024
R break and next
We use the R break and next statements to alter the flow of a
program. These are also known as jump statements in programming:
break - terminate a looping statement
next - skips an iteration of the loop

12/12/2024
R break Statement

You can use a break statement inside a loop (for, while, repeat)
to terminate the execution of the loop. This will stop any
further iterations.
The syntax of the break statement is:

The break statement is often used inside a conditional (if...else) statement


in a loop. If the condition inside the test_expression returns True, then the
break statement is executed. For example,
12/12/2024
R break Statement
Example:

Here, we have defined a vector of


numbers from 1 to 7. Inside
the for loop, we check if the
current number is 4 using
an if statement.
If yes, then the break statement is
executed and no further iterations
are carried out. Hence, only
numbers from 1 to 3 are printed.

12/12/2024
break Statement in Nested Loop
If you have a nested loop and the break statement is inside the inner loop, then
the execution of only the inner loop will be terminated.

• Here, we have a break statement


inside the inner loop.
• We have used it inside a
conditional statement such that if
both the numbers are equal to 2,
the inner loop gets terminated.
• The flow then moves to the outer
loop. Hence, the combination (2,
2) is never printed.

12/12/2024
R next Statement
In R, the next statement skips the current iteration of the loop and starts the loop
from the next iteration.
The syntax of the next statement is:

If the program encounters the next statement, any further execution of code
from the current iteration is skipped, and the next iteration begins.

12/12/2024
R next Statement

Here, we have used an if


statement to check whether
the current number in the
loop is odd or not.

If yes, the next statement


inside the if block is
executed, and the current
iteration is skipped.

12/12/2024
R repeat Loop
• We use the R repeat loop to execute a code block multiple times. However, the
repeat loop doesn't have any condition to terminate.
• You can use the repeat loop in R to execute a block of code multiple times.
• However, the repeat loop does not have any condition to terminate the loop. You
need to put an exit condition implicitly with a break statement inside the loop. The
syntax of repeat loop is:
Here, we have used the
repeat keyword to create a
repeat loop.
It is different from the for
and while loop because it
does not use a predefined
condition to exit from the
loop.
12/12/2024
R repeat Loop
• Example 1: R repeat Loop
• Let's see an example that will print numbers using a repeat loop and will execute until the
break statement is executed.

Here, we have used a


repeat loop to print
numbers from 1 to 5.
We have used an if
statement to provide a
breaking condition which
breaks the loop if the
value of x is greater than
4.

12/12/2024
R repeat Loop
• Example 2: Infinite repeat Loop
• If you fail to put a break statement inside a repeat loop, it will lead to an infinite loop. For
example,

In the above program,


since we have not
included any break
statement with an exit
condition, the
program prints the
sum of numbers
infinitely.

12/12/2024
R repeat Loop
• Example 3: repeat Loop with next Statement
• You can also use a next statement inside a repeat loop to skip an iteration. For example,

Here, we have a repeat loop


where we break the loop if
x is equal to 4. We skip the
iteration where x becomes
equal to 2.

12/12/2024
R Data Structures
R Vectors
A vector is the basic data structure in R that stores data of
similar types. For example,
Suppose we need to record the age of 5 employees. Instead of
creating 5 separate variables, we can simply create a vector.

12/12/2024
R Data Structures
R Vectors
Create a Vector in R
In R, we use the c() function to create a vector. For example,

In the above example, we have created a vector named employees with elements: Sabby, Cathy,
and Lucy.
Here, the c() function creates a vector by combining three different elements
of employees together.

12/12/2024
R Data Structures
R Vectors
• Access Vector Elements in R
• In R, each element in a vector is associated with a number. The number is known as a vector
index.
• We can access elements of a vector using the index number (1, 2, 3 …). For example,

In the above example, we have


created a vector named languages.
Each element of the vector is
associated with an integer number.

12/12/2024
R Data Structures
R Vectors

Here, we have used the vector index to access the vector elements
languages[1] - access the first element "Swift"
languages[3] - accesses the third element "R“

Note: In R, the vector index always starts with 1. Hence, the first element of a vector is
present at index 1, second element at index 2 and so on.

12/12/2024
R Data Structures
R Vectors
Modify Vector Element
To change a vector element, we can simply reassign a new value to the specific index. For
example,

Here, we have changed


the vector element at
index 2 from "Repeat" to
"Sleep" by simply
assigning a new value.

12/12/2024
R Data Structures
R Vectors
Numeric Vector in R
Similar to strings, we use the c() function to create a numeric vector. For
example,

Here, we have used the C() function to create a vector of numeric sequence
called numbers.

12/12/2024
R Data Structures
R Vectors
Create a Sequence of Number in R
In R, we use the : operator to create a vector with numerical values in sequence. For
example,
Here, we have used
the : operator to
create the vector
named numbers
with numerical
values in sequence
i.e. 1 to 5.

12/12/2024
R Data Structures
R Vectors
Repeat Vectors in R
In R, we use the rep() function to repeat elements of vectors. For example,

In the above example, we have created a numeric vector with elements 2, 4, 6. Notice the code,

12/12/2024
R Data Structures
R Vectors
Repeat Vectors in R
In R, we use the rep() function to repeat elements of vectors. For example,

In the above example, we have created a numeric vector with elements 2, 4, 6. Notice the code,

We can see that we have repeated


the whole vector two times.
However, we can also repeat each
element of the vector. For this we
use the each parameter.
12/12/2024
R Data Structures
R Vectors
Loop Over a R Vector
We can also access all elements of the vector by using a for loop. For example,
In R, we can also loop through each element of the vector using the for loop. For example,

12/12/2024
R Data Structures
R Vectors
Length of Vector in R
We can use the length() function to find the number of elements present inside the
vector. For example,

Here, we have used


length() to find the
length of the
languages vector.

12/12/2024
R Data Structures
R Lists
A List is a collection of similar or different types of data.
In R, we use the list() function to create a list. For example,

12/12/2024
R Data Structures
R Lists
Access List Elements in R
In R, each element in a list is associated with a number. The number is known as a list
index.
We can access elements of a list using the index number (1, 2, 3 …). For example,

Note: In R, the list index


always starts with 1.
Hence, the first element
of a list is present at
index 1, second element
at index 2 and so on.

12/12/2024
R Data Structures
Modify a List Element in R
R Lists
To change a list element, we can simply reassign a new value to the specific index. For
example,

12/12/2024
R Data Structures
R Lists
Add Items to R List
We use the append() function to add an item at the end of the list. For example,

12/12/2024
R Data Structures
R Lists
Remove Items From a List in R
R allows us to remove items for a list. We first access elements using a list index and
add negative sign - to indicate we want to delete the item. For example,
[-1] - removes 1st item
[-2] - removes 2nd item and so on.

12/12/2024
R Data Structures
R Lists
Remove Items From a List in R
R allows us to remove items for a list. We first access elements using a list index and
add negative sign - to indicate we want to delete the item. For example,
[-1] - removes 1st item
[-2] - removes 2nd item and so on.

12/12/2024
R Data Structures
R Lists
Length of R List
In R, we can use the length() function to find the number of elements present inside the
list. For example,

Here, we have used


the length() function
to find the length
of list1. Since there
are 4 elements
in list1 so length() ret
urns 4.

12/12/2024
R Data Structures
R Lists
Loop Over a List
In R, we can also loop through each element of the list using the for loop. For example,

12/12/2024
R Data Structures
R Lists
Check if Element Exists in R List
In R, we use the %in% operator to check if the specified element is present in the list or not and
returns a Boolean value.
TRUE - if specified element is present in the list
FALSE - if specified element is not present in the list

12/12/2024
R Data Structures
R Matrix

A matrix is a two-dimensional data structure where data are arranged into rows and
columns. For example,

Here, the above matrix is 3 * 3 (pronounced "three by three") matrix because it has 3 rows
and 3 columns.

12/12/2024
R Data Structures
R Matrix
Create a Matrix in R
In R, we use the matrix() function to create a matrix.
The syntax of the matrix() function is

12/12/2024
R Data Structures
R Matrix
Create a Matrix in R
In R, we use the matrix() function to create a matrix.
The syntax of the matrix() function is

In the above example,


we have used
the matrix() function
to create a matrix
named matrix1

12/12/2024
R Data Structures
R Matrix

Here, we have passed data items of integer type and used c() to combine data items
together. And nrow = 2 and ncol = 3 means the matrix has 2 rows and 3 columns.
Since we have passed byrow = TRUE, the data items in the matrix are filled row-wise.
If we didn't pass byrow argument as

12/12/2024
R Data Structures
R Matrix
Access Matrix Elements in R
We use the vector index operator [ ] to access specific elements of a matrix in R.
The syntax to access a matrix element is

In the above example, we have


created a 2 by 2 matrix
named matrix1 with 4 string type
datas. Notice the use of index
operator [],

12/12/2024
R Data Structures
R Matrix
Access Entire Row or Column
In R, we can also access the entire row or column based on the value passed inside [].
[n, ] - returns the entire element of the nth row.
[ ,n] - returns the entire element of the nth column.
For example,

Here,
matrix1[1, ] - access entire
elements at 1st row
i.e. Sabby and Larry
matrix1[ ,2] - access entire
elements at 2nd column
i.e. Larry and Harry

12/12/2024
R Data Structures
R Matrix
Access More Than One Row or Column
We can access more than one row or column in R using the c() function.
[c(n1,n2), ] - returns the entire element of n1 and n2 row.
[ ,c(n1,n2)] - returns the entire element of n1 and n2 column.
For example,

Here,
[c(1,3), ] - returns the
entire element
of 1st and 3rd row.
[ ,c(2,3)] - returns the
entire element
of 2nd and 3rd colum
n.

12/12/2024
R Data Structures
R Matrix
Modify Matrix Element in R
We use the vector index operator [] to modify the specified element. For example,

Here, the element present at 1st row, 2nd column is changed to 140.
Let's see an example,

12/12/2024
R Data Structures
R Matrix
Combine Two Matrices in R
In R, we use the cbind() and the rbind() function to combine two matrices together.
cbind() - combines two matrices by columns
rbind() - combines two matrices by rows
The number of rows and columns of two matrices we want to combine must be equal. For example,

Here, first we have used the cbind() function to combine the two
matrices: even_numbers and odd_numbers by column. And rbind() to combine two matrices by row.
12/12/2024
R Data Structures
R Matrix
Check if Element Exists in R Matrix
In R, we use the %in% operator to check if the specified element is present in the matrix or not and
returns a Boolean value.
TRUE - if specified element is present in the matrix
FALSE - if specified element is not present in the matrix
For example,

Here,
"Larry" is present in matrix1,
so the method returns TRUE
"Kinsley" is not present
in matrix1, so the method
returns FALSE

12/12/2024

You might also like