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

Introduction To R in Data Analytics

Module-2 of the CSE1006 course covers the introduction to R, including installation, basic operations, data types, and functions. It explains variable assignment, operators, control statements, and loops in R, providing examples for clarity. The module aims to equip students with foundational skills in data analytics using R programming.

Uploaded by

yaraha5692
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)
5 views135 pages

Introduction To R in Data Analytics

Module-2 of the CSE1006 course covers the introduction to R, including installation, basic operations, data types, and functions. It explains variable assignment, operators, control statements, and loops in R, providing examples for clarity. The module aims to equip students with foundational skills in data analytics using R programming.

Uploaded by

yaraha5692
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
You are on page 1/ 135

Course code : CSE1006

Course title : Foundations of Data Analytics

Module-2
Introduction to R

24-01-2025 Dr. V. Srilakshmi


MODULE-2
• Introduction to R: R installation, Basic operations in R using
command line, use of IDE R Studio, ‘R help’ feature in R, Data
types and function, Variables in R, Scalars, Vectors, Matrices, List,
Data frames, functions in R, Factors

24-01-2025 Dr. V. Srilakshmi


24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
Basic operations in R
• Unlike many other programming languages, you can output
code in R without using a print function.
• To output text in R, use single or double quotes.
• Ex1: "Hello World!“
Output: [1] “Hello World!“
• Ex2: ‘Hello World!’
Output: [1] ‘Hello World!’
• To output numbers, just type the number (without quotes)
• Ex3: 5
Output: [1] 5
24-01-2025 Dr. V. Srilakshmi
• To do simple calculations, add numbers together
• Ex3: 5+3
Output: [1] 8
• R does have a print() function to output the code.
• Ex: print("Hello World!")
• Output: [1] “Hello World!“

24-01-2025 Dr. V. Srilakshmi


R Variables:
• Variables are containers for storing data values.
• R does not have a command for declaring a variable. A variable is
created the moment you first assign a value to it. To assign a value
to a variable, use the Assignment Operator.
• To output (or print) the variable value, just type the variable name.
• Example:
>name = “VIT AP"
>YOE <-=2018

>name # output “VIT AP"


>YOE # output 2018
24-01-2025 Dr. V. Srilakshmi
• print() or cat() functions can be used to output
variable value
• Print (name) or cat(name) # output “VIT AP“
• Print (name,YOE) # output “VIT AP 2018“
• Print (name,”Amaravati”) # output “VIT AP
Amaravati“
• Print (“Name=“,name) # output “Name =
VIT AP“

24-01-2025 Dr. V. Srilakshmi


Different ways of assigning a value to variable:
• There are 4 types of assignment operators in R
1. =
2. <-
3. ->
4. assign()
Example:
name = “VIT AP“

name <- “VIT AP“

“VIT AP”->name

assign(“name”, “VIT AP“)


24-01-2025 Dr. V. Srilakshmi
• The currently used variables available in workspace can
be listed using ls() function.
Example:
>ls()
Variable name that begin with .(dot) are called hidden
variables and can be shown using
>ls(all.names-True)

24-01-2025 Dr. V. Srilakshmi


Removing Variables:
• It is easily done using remove or its shortcut rm.
Example:
>a=10
>a
[1] 10
>rm(a) #removes variable a

• To delete all the variables we use


>rm(list=ls()) # hidden variables are not deleted
>rm(list=ls(all.names=True)) # deletes all variables
including hidden variables
24-01-2025 Dr. V. Srilakshmi
Rules to naming a R Variable:
• A variable name must start with a letter and can be a combination of
letters, digits, period(.) and underscore(_). If it starts with period(.), it
cannot be followed by a digit.
• A variable name cannot start with a number or underscore (_)
• Variable names are case-sensitive (age, Age and AGE are three different
variables)
• Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
• # Legal variable names:
• Myvar, my_var, myVar, MYVAR, Myvar2, .myvar
• # Illegal variable names:
• 2myvar, my-var, my var, _my_var, my_v@ar, TRUE
24-01-2025 Dr. V. Srilakshmi
R Data Types
• Variables can store data of different types, and different types
can do different things.
• In R, variables do not need to be declared with any particular
type and can even change type after they have been set.
• Basic data types in R can be divided into the following types:
• numeric - (10.5, 55, 787)
• integer - (1L, 55L, 100L, where the letter "L" declares this as
an integer)
• complex - (9 + 3i, where "i" is the imaginary part)
• character (string) - ("k", "R is exciting", "FALSE", "11.5")
• logical (boolean) - (TRUE or FALSE)
24-01-2025 Dr. V. Srilakshmi
Dealing with dates and times can be done in R using
• Date()-stores just a date in format(“yyyy-mm-dd”)
• POSIXct()-stores date and time in format(“yyyy-mm-dd HH:MM:SS”)
Example:
>DOB=as.Date(“2012-06-28”)
>DOB
[1] “2012-06-28”
>class(DOB)
[1]”Date”
>as.numeric(DOB)
[1] 15519

24-01-2025 Dr. V. Srilakshmi


>date2<-as.POSIXct(“2012-06-28 17:42”)
>Date2
>[1] “2012-06-28 05:42:00 IST”

24-01-2025 Dr. V. Srilakshmi


• The class function is used to chek the data type of a
variable.
• x <- 10.5 x <- "R is exciting"
class(x) #Numeric class(x)#Charater

x <- 1000L x <- TRUE


class(x) #Integer class(x)#Boolean

x <- 9i + 3
class(x) #complex

24-01-2025 Dr. V. Srilakshmi


Operators in R
• Operators are the symbols directing the compiler to perform various
kinds of operations between the operands.
• Types of the operator in R language
• Arithmetic Operators
• Logical Operators
• Relational Operators
• Assignment Operators
• Miscellaneous Operator
• Arithmetic Operators:
• Arithmetic operations in R simulate various math
operations, like addition, subtraction,
multiplication, division, and modulo using the
specified operator between operands.
• Operands may be either scalar values, complex
numbers, or vectors.
24-01-2025 Dr. V. Srilakshmi
• Example : Arithmetic.R
1. # R Program to demonstrate Arithmetic operators
2. a=10
3. b=20
4. cat("SUM=",a+b)
5. cat("Subtration=",a-b)
6. cat("Multipliation=",a*b)
7. cat("Division=",a/b)
8. cat("reminder=",a%%b)
9. cat("Integer division=",a%/%b)
• > Rscript Arithmetic.R
SUM= 30Subtration= -10Multipliation= 200Division= 0.5reminder=
10Integer reminder= 0
24-01-2025 Dr. V. Srilakshmi
• Assignment Operators:
• Assignment operators are used to assign values to variables.
• In R, we can use =,->,->>,<-,<<- as an Assignment operator.
• Example: A=10 (or) A<-10 (or) A<<-10 (or) 10->>A (or) 10->A
• It is possible to assign same value to multiple variables at a time.
• Example: A<-B<-C<-10

• Relational Operators:
• Relational operators are used to compare two values.
• Returns a boolean TRUE value if the first operand satisfies the relation
compared to the second and Returns FALSE otherwise.
• A TRUE value is always considered to be greater than the FALSE.
• R supports these relational operators <,>,<=,>=,==,!=
24-01-2025 Dr. V. Srilakshmi
• Logical Operators:
• Logical operators are used to combine conditional statements.
• Logical operations in R simulate element-wise decision operations, based on
the specified operator between the operands, which are then evaluated to
either a True or False boolean value.
• Any non-zero integer value is considered as a TRUE value, be it a complex or
real number.

24-01-2025 Dr. V. Srilakshmi


• &- Returns True if both the operands are True.
• |- Returns True if either of the operands is True.
• !- A unary operator that negates the status of the elements of the operand.
• && - Returns True if both the first elements of the operands are True.
• || - Returns True if either of the first elements of the operands is True.

• Example: list1 <- c(TRUE, 0.1)


list2 <- c(0,4+3i)
print(list1 & list2) #Output : False True
print(list1 | list2) #Output : True True
print(!list1) #Output : False False
print(list1 && list2) #Output : False
print(list1 || list2) #Output : True

24-01-2025 Dr. V. Srilakshmi


• R Miscellaneous Operators:
• Miscellaneous operators are used to manipulate data

24-01-2025 Dr. V. Srilakshmi


Taking Input from User in R
• In R, readline() method allows user to provide input to the
code interactively.
• readline() method takes input in string format.
• Example: var = readline();
• To convert the inputted value to the desired data type, there
are some functions in R
• as.integer(n); —> convert to integer
• as.numeric(n); —> convert to numeric type (float, double etc)
• as.complex(n); —> convert to complex number (i.e 3+2i)
• as.Date(n) —> convert to date …, etc
24-01-2025 Dr. V. Srilakshmi
• Example:
print("enter some data");
var=readline();
var=as.integer(var);
print(var);
(OR)

• Example:
var=as.integer(readline("enter some data"));
print(var);
24-01-2025 Dr. V. Srilakshmi
EXAMPLE PROGRAM:
# R PROGRAM TO FIND AREA AND PERIMETER OF A RECTANGLE.
l=as.integer(readline("enter length"))
b=as.integer(readline("enter breadth"))
Area=l*b
Pm=2*(l+b)
cat("Area=",Area)
cat("\nPertimerter=",Pm)

24-01-2025 Dr. V. Srilakshmi


Control statements
• Control statements are expressions used to control the execution and
flow of the program based on the conditions provided in the statements.

1. if condition: This control structure checks the expression provided in


parenthesis is true or not. If true, the execution of the statements in
braces {} continues.

Syntax: if(expression){
statements
....
....
}

24-01-2025 Dr. V. Srilakshmi


1. if – else condition: It is similar to if condition but when the test
expression in if condition fails, then statements in else condition are
execute.
Syntax: if(expression){
statements
....
}
else{
statements
....
}
24-01-2025 Dr. V. Srilakshmi
Control statements
3. if-else-if ladder:
Syntax: if(condition 1 is true) {
execute this statement
} else if(condition 2 is true) {
execute this statement
}
…………
else {
execute this statement#This block is
executed when none of the above conditions are true.
}
24-01-2025 Dr. V. Srilakshmi
4. Nested if – else condition:
Syntax: if(expression) #Parent
Condition
{
if-else statement #Child condition
}
else{

if-else statement #Child condition


}

24-01-2025 Dr. V. Srilakshmi


Control statements
Example:# R Nested if else statement Example
if(a > b){
if(a >c){
print(“a is big")
}
else{
print(“c is big")
}
}
else{
if(b>c){
print(“b is big")
}
else{
print(“c is big")
}
} 24-01-2025 Dr. V. Srilakshmi
Control statements

5. Switch Statement:
• In this switch function expression is matched to list of cases.
• If a match is found, then it prints that case’s value.
• No default case is available here.
• If no case is matched it outputs NULL.
Syntax: switch (expression, case1, case2, case3,…,case n )

24-01-2025 Dr. V. Srilakshmi


• Eg1 :
# Expression in terms of the index value

x <- switch(2,“VIT AP",“University“, “Amaravati")


print(x)

Output:
University

24-01-2025 Dr. V. Srilakshmi


Control statements
5. Switch Statement:
• Eg3 : # Expression in terms of the string value
z <- switch(
“place”,
”name”=“VIT AP”,
“title”=“University”
) print(z) output: NULL
Ex3:
z <- switch(
“place”,
”name”=“VIT AP”,
“title”=“University”
“place”=“Amaravati”
) print(z) output:Amaravati
24-01-2025 Dr. V. Srilakshmi
LOOPS
• Loops can execute a block of code as long as a specified
condition is reached.
• Loops save time, reduce errors, and they make code more
readable.
• R has two loop commands:
• while loops
• for loops

24-01-2025 Dr. V. Srilakshmi


• While Loop:
• Syntax:
while (test_expression)
{
statement(s)
update expression
}

24-01-2025 Dr. V. Srilakshmi


LOOPS
• For Loop:
• For loop in R is useful to iterate over the elements of a
list, data frame, vector, matrix, or any other object.
• Syntax:

for (Var in vector){


statement(s)
}

24-01-2025 Dr. V. Srilakshmi


• Repeat Loop:
• 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.
• Syntax:
repeat {
commands
if (condition) {
break
}
}

24-01-2025 Dr. V. Srilakshmi


Example:
x=1 ➢source("~/sam.r")
➢[1] 1
repeat ➢[1] 2
➢[1] 3
{ ➢[1] 4
➢[1] 5
print(x)
x=x+1
if(x==6)
{
break
}
}
24-01-2025 Dr. V. Srilakshmi
Break and Next statements
• The basic function of the Break and Next statement
is to alter the running loop in the program and flow
the control outside of the loop.
• break statement:
• The break Statement in R is a jump statement that is
used to terminate the loop at a particular iteration.
• Syntax: break

24-01-2025 Dr. V. Srilakshmi


• Example:
N=1:10
for(val in N)
Output:
{ [1] 1
[1] 2
if(val==5) [1] 3
[1] 4
{
break
}
print(val)
cat(”\n”);
}
24-01-2025 Dr. V. Srilakshmi
Break and Next statements
• next statement:
• The next statement in R is used to skip the current
iteration in the loop and move to the next iteration
without exiting from the loop itself.
• This is similar to continue statement in C language.
• Syntax: next

24-01-2025 Dr. V. Srilakshmi


• Example:
N=1:7
for(val in N) Output:
[1] 1
{ [1] 2
if(val==5) [1]
[1]
3
4
{ [1] 6
[1] 7
next
}
print(val)
cat(”\n”);
}
24-01-2025 Dr. V. Srilakshmi
Functions
• A function is a block of code which only runs when it is called.
• Functions are useful when you want to perform a certain task multiple
times. A function accepts input arguments and returns the output by
executing valid R commands that are inside the function.
• Creating a function:
• Functions are created in R by using the command function().
• Syntax:
F=function(arguments) #Here, F is a function name.
{
Statements
}
• Statements in function() runs when it is called.
• To call a function, use the function name followed by parenthesis, like F()
24-01-2025 Dr. V. Srilakshmi
Types of Function
• Built-in Function: Built-in functions in R are pre-defined functions that are
available in R programming language to perform common tasks or
operations.
• Examples:
• # Find sum of numbers 4 to 6.
• print(sum(4:6)) #output : 15

• # Find max of numbers 4 and 6.


• print(max(4:6)) #output : 6

• # Find min of numbers 4 and 6.


• print(min(4:6)) #output : 4

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


24-01-2025 Dr. V. Srilakshmi
• Example:
# A simple R function to check whether x is even or odd
evenOdd = function(x)
{
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4)) #function call
print(evenOdd(3))
• Output:
[1] even
[1] odd

24-01-2025 Dr. V. Srilakshmi


• Example:
# A simple R function to calculate area and perimeter of a rectangle
Rectangle = function(length, width)
{
area = length * width
perimeter = 2 * (length + width)
# creating an object called result which is a list of area and perimeter
result = list("Area" = area, "Perimeter" = perimeter)
return(result)
}
resultList = Rectangle(2, 3)
print(resultList["Area"])
print(resultList["Perimeter"])
• Output:
[1] 6
[1] 10
24-01-2025 Dr. V. Srilakshmi
Data Structures in R
• A data structure is a particular way of organizing data in a computer so
that it can be used effectively.
• The idea is to reduce the space and time complexities of different tasks.
Data structures in R programming are tools for holding multiple values.
• R’s base data structures are often organized by their dimensionality (1D,
2D, or nD) and whether they’re homogeneous (all elements must be of
the identical type) or heterogeneous (the elements are often of various
types).
• The most essential data structures used in R include:
• Vectors
• Lists
• Dataframes
• Matrices
• Arrays
• Factors
24-01-2025 Dr. V. Srilakshmi
Vectors
• A vector is simply a list of items that are of the same type.
• Vectors are one-dimensional data structures.
• Example:
• # Vector of strings
>fruits <- c("banana", "apple", "orange")
# Print fruits
>fruits #output: [1] “banana” “apple”
“orange”

• To create a vector with numerical values in a sequence, use the : operator.


• Example:
• # Vector with numerical values in a sequence
>numbers <- 1:10
>numbers #output: [1] 1 2 3 4 5 6 7 8 9 10
24-01-2025 Dr. V. Srilakshmi
Vectors
• Use the seq() function to make bigger or smaller steps in a sequence.
• The seq() function has three parameters: “from” is where the sequence
starts, “to” is where the sequence stops and “by” is the interval of the
sequence.
• Example:
>numbers <- seq(from = 0, to = 100, by = 20)
Or
>numbers <- seq(0,100,20)

>numbers #output: [1] 0 20 40 60 80 100

24-01-2025 Dr. V. Srilakshmi


Write a R program to create a vector of a specified type and length. Create
vector of numeric, complex, logical and character types of length 6.

• x = vector("numeric", 5)

• print("Numeric Type:")

• print(x)

• c = vector("complex", 5)

• print("Complex Type:")

• print(c)

24-01-2025 Dr. V. Srilakshmi


• l = vector("logical", 5)

• print("Logical Type:")

• print(l)

• chr = vector("character", 5)

• print("Character Type:")

• print(chr)

24-01-2025 Dr. V. Srilakshmi


Vectors
• You can also create numerical values with decimals in a sequence, but note that if
the last element does not belong to the sequence, it is not used. Vectors are one-
dimensional data structures.
• Example:
• # Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1 #output: [1] 1.5 2.5 3.5 4.5 5.5 6.5
# Vector with numerical decimals in a sequence where the last
element is not used
numbers2 <- 1.5:6.3
numbers2 #output: [1] 1.5 2.5 3.5 4.5 5.5
• It is also possible to create a vector with Boolean values
• Example:
• # Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)
log_values #output: [1] TRUE FALSE TRUE FALSE
24-01-2025 Dr. V. Srilakshmi
Vectors
• To find out how many items a vector has, use length() function.
• Example:
• #finding number of items in a vector
• fruits <- c("banana", "apple", "orange")

length(fruits) #output: [1] 3


• To sort items in a vector alphabetically / numerically, use the sort() function.
• Example:
• #sorting a vector in ascending order
fruits <- c("banana", "apple", "orange“)
numbers <- c(13, 3, 5, 7)

sort(fruits) # Sort a string #output: [1] “apple” “banana” “orange”


sort(numbers) # Sort numbers #output: [1] 3 5 7 13

24-01-2025 Dr. V. Srilakshmi


• To sort items in a vector alphabetically / numerically, use the
sort() function.
• Example:
• #sorting a vector in descending order

numbers <- c(13, 3, 5, 7)

sort(numbers, decreasing=TRUE) # Sort numbers


#output: [1] 13 7 5 3

24-01-2025 Dr. V. Srilakshmi


Vectors
• You can access the vector items by referring to its index number inside
brackets []. The first item has index 1, the second item has index 2.
• Example:
• fruits <- c("banana", "apple", "orange")
# Access the first item (banana)
fruits[1] #output: [1] “banana”
fruits[c(1, 3)] #output: [1] “banana” “orange”
• You can also use negative index numbers to access all items except the ones
specified.
• Example:
• #to access all items except specified
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Access all items except for the first item
fruits[c(-1)] #output:[1]"apple“ "orange" "mango“ "lemon"
24-01-2025 Dr. V. Srilakshmi
Vectors
• To change the value of a specific item, refer to the index number
• Example:
• fruits <- c("banana", "apple", "orange")
# Change "banana" to "pear"
fruits[1] <- "pear"
# Print fruits
fruits #output: [1] “pear” “apple” “orange”

• To repeat vectors, use the rep() function.


• Example:
• repeat_each <- rep(c(1,2,3), each = 3)
repeat_each #output:[1] 1 1 1 2 2 2 3 3 3
• repeat_times <- rep(c(1,2,3), times = 3)
repeat_times #output:[1] 1 2 3 1 2 3 1 2 3
24-01-2025 Dr. V. Srilakshmi
We can perform all arithmetic operations on vectors.
Write a R program to add two vectors of integers type and length 3

x = c(10, 20, 30)

y = c(20, 10, 40)

z=x+y

print(z)

Output:

[1] 30 30 70
24-01-2025 Dr. V. Srilakshmi
Write a R program to append value to a given vector.
x = c(10, 20, 30)
x=c(x,34)
print(x)
Output:
[1] 10 20 30 34

24-01-2025 Dr. V. Srilakshmi


• to test whether a given vector contains a specified element.

• x = c(10, 20, 30, 25, 9, 26)


• is.element(25, x)
Output:TRUE

24-01-2025 Dr. V. Srilakshmi


• to count the specific value in a given vector.

• x = c(10, 20, 30, 20, 20, 25, 9, 26)

• print("Original Vectors:")

• print(x)

• print("Count specific value(20) in above vector:")

• print(sum(x==20))

• Output: 3

24-01-2025 Dr. V. Srilakshmi


• to find common elements from multiple vectors.

• x = c(10, 20, 30, 20, 20, 25, 29, 26)

• y = c(10, 50, 30, 20, 20, 35, 19, 56)

• intersect(x,y)

• z = c(10, 40, 30, 20, 20, 25, 49, 26)


• result = intersect(intersect(x,y),z)

• print(result)

24-01-2025 Dr. V. Srilakshmi


• to list the distinct values in a vector from a given vector.
• v = c(10, 10, 10, 20, 30, 40, 40, 40, 50)
• print(unique(v))
• to reverse the order of given vector
• rev(v)

24-01-2025 Dr. V. Srilakshmi


• To find Sum, Mean,Median,standard deviation and
Product of a Vector
>v=c(2,4,7,8,1)
>v
[1] 2 4 7 8 1
> mean(v)
[1] 4.4
>sum(v) [1]
22
> median(v)
[1] 4
> sd(v)
[1] 3.04959
> prod(v)
[1] 448
24-01-2025 Dr. V. Srilakshmi
Using loops on vectors:

for(x in 1:5)
{
print(x)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
24-01-2025 Dr. V. Srilakshmi
Example: Output:
[1] "Winter, month 1"
[1] "Winter, month 2"
for (month in 1:5) [1] "Spring, month 3"
{ [1] "Spring, month 4“
if (month < 3) [1] "Spring, month 5"
{
print(paste('Winter, month', month))
}
else {
print(paste('Spring, month', month))
}
}
24-01-2025 Dr. V. Srilakshmi
Lists
• A list in R is a generic object consisting of an ordered collection of objects.
Lists are one-dimensional, heterogeneous data structures.
• The list can be a list of vectors, a list of matrices, a list of characters and a list
of functions, and so on.
• To create a List in R you need to use the function called “list()”.
• Example:
• # List of strings
thislist <- list("apple", "banana", "cherry")
# Print the list
thislist
• You can access the list items by referring to its index number, inside brackets.
The first item has index 1, the second item has index 2 and so on.
• Example:
• thislist <- list("apple", "banana", "cherry")
thislist[1] #Output: [[1]]
24-01-2025 Dr. V. Srilakshmi
[1] apple
Lists
• To change the value of a specific item, refer to the index number.
• Example:
• thislist <- list("apple", "banana", "cherry")
thislist[1] <- "blackcurrant"
# Print the updated list
thislist

• To findout how many items a list has, use length() function


• Example:
• thislist <- list("apple", "banana", "cherry")
length(thislist) #Output: [1] 3
• To find out if a specified item is present in a list, use the %in% operator
• thislist <- list("apple", "banana", "cherry")
"apple" %in% thislist #Output: [1] TRUE
24-01-2025 Dr. V. Srilakshmi
Lists
• To add an item to the end of the list, use the append() function. After
appending it print the newlist by default.
• Example:
• thislist <- list("apple", "banana", "cherry")
append(thislist, "orange")

• To add an item to the right of a specified index, add "after=index number" in


the append() function
• Example:
• thislist <- list("apple", "banana", "cherry")
append(thislist, "orange", after = 2) #Output : "apple“ "banana“ “orange” "cherry"

24-01-2025 Dr. V. Srilakshmi


Lists
You can also remove list items. The following example creates a new, updated list
without an "apple" item.
Example:
• thislist <- list("apple", "banana", "cherry")
newlist <- thislist[-1]
newlist #Output : "banana“ "cherry“
• Note that there is no change in thislist

You can specify a range of indexes by specifying where to start and where to end the
range, by using the : operator
Example:
• thislist=list("apple", "banana", "cherry", "orange", "kiwi", "melon")

(thislist)[2:5] #Output : "banana“ "cherry“ "orange"


24-01-2025 Dr. V. Srilakshmi
Lists
• You can also use a list in loops to access items of loop on after another.
• Example:
• thislist <- list("apple", "banana", "cherry")
for (x in thislist)
{
print(x)
}
• You can join two loops using c() function.
• Example:
• list1 <- list("a", "b", "c")
list2 <- list(1,2,3)
list3 <- c(list1,list2)
list3

24-01-2025 Dr. V. Srilakshmi


Matrices
• To create a matrix in R you need to use the function called matrix(). The
arguments to this matrix() are the set of elements in the vector.
• In R programming, matrices are two-dimensional, homogeneous data
structures.
• Specify the nrow and ncol parameters to get the amount of rows and
columns.
• By default, matrices are in column-wise order. To order row-wise set byrow =
TRUE.
• Example 1:
• # Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)

# Print the matrix


thismatrix
24-01-2025 Dr. V. Srilakshmi
• We can define row and column names by using the functions
rownames() and colnames()
r=c(“r1”,”r2”,”r3”,”r4”)
r=c(“c1”,”c2”,”c3”)
M=matrix(3:14,nrow=4,ncol=3,dimnames=list(r,c))
(Or)
M=matrix(3:14,nrow=4,ncol=3)
Rownames(M)=c(“r1”,”r2”,”r3”,”r4”)
Colnames(M)=c(“c1”,”c2”,”c3”)

24-01-2025 Dr. V. Srilakshmi


Matrices
• Example 2:
• # Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2,byrow = TRUE)

# Print the matrix


thismatrix
• You can also create a matrix with strings
• Example 3:
• thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2)
thismatrix
• You can access the items by using [ ] brackets. The first number "1" in the
bracket specifies the row-position, while the second number "2" specifies the
column-position.
• Thismatrix[1,2] #[1] “cherry”
24-01-2025 Dr. V. Srilakshmi
Matrices
• The whole row can be accessed if you specify a comma after the number in the
bracket.
• The whole column can be accessed if you specify a comma before the number in the
bracket.
• Example 2:
• # Create a matrix
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2
thismatrix[2,] # Accessing 2nd row elements of a matrix
thismatrix[,2] # Accessing 2nd row elements of a matrix
thismatrix[c(1,2),] # Accessing 1st and 2nd row elements of a matrix
thismatrix[,c(1,2)] # Accessing 1st and 2nd column elements of a matrix
• Use the c() function with negative(–) sign to remove rows and columns in a Matrix

24-01-2025 Dr. V. Srilakshmi


Matrices
• The function rbind() is used to add additional rows in a Matrix and cbind() is
used to add additional columns in a Matrix.
• Example:
matrixA=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
matrixA
matrixB=rbind(matrixA,c(10,11,12))
matrixB
matrixA #No change in matrixA
matrixC=cbind(matrixA,c(10,11,12))
matrixC

matrixD = matrixC[-c(1),-c(1)]
matrixD
24-01-2025 Dr. V. Srilakshmi
Matrices
• Use the dim() function to find the number of rows and columns in a Matrix.
• Example:
matrixA=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
matrixA
dim(matrixA)
cat("number of rows = ",nrow(matrixA))
cat("\nnumber of rows = ",ncol(matrixA))

• Use the length() function to find the number of elements in a Matrix.


• Example:
• length(matrixA) #output : 9
24-01-2025 Dr. V. Srilakshmi
Matrices
• You can use for loop to access elements in a in a Matrix.
• Example:
matrixA=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
matrixA
#accessing matrix elements
for(rows in 1:nrow(matrixA))
{
for(cols in 1:ncol(matrixA))
{
print(matrixA[rows,cols])
}
}
24-01-2025 Dr. V. Srilakshmi
Matrices
• You can use rbind() and cbind() to combine 2 matrices.
• Example:
matrixA=matrix(c(1,1,1,1,1,1,1,1,1),nrow=3,ncol=3)
matrixB=matrix(c(2,2,2,2,2,2,2,2,2),nrow=3,ncol=3)

newmatrixA=rbind(matrixA,matrixB)
newmatrixA

newmatrixA=cbind(matrixA,matrixB)
newmatrixA

24-01-2025 Dr. V. Srilakshmi


Matrix operations:
• If A and B are two matrices of order 3x2 then
C=A+B # MATRIX ADDITION
C=A-B # MATRIX SUBTRACTION
C=A/B # MATRIX DIVISION
C=A*B # MATRIX MLTIPLICATION
If If A and B are two matrices of order mxn and nxp then
matrix multiplication results a matrix of order mxp. It is done
as
C=A%*%B
24-01-2025 Dr. V. Srilakshmi
• Transpose: t() function is used to find the transpose of a matrix.
>t(A) where A is a matrix of order mxn
• Exponent:
>A^2
>A^B
• Determinant: If A is a square matrix then det() is a function to find
determinant of A.
>det(A)
• Inverse: If A is a square matrix then solve() is a function to
find inverse of A.
>solve(A)
24-01-2025 Dr. V. Srilakshmi
Arrays
• Compared to matrices, arrays can have more than two dimensions.
• We can use the array() function to create an array, and the dim parameter to
specify the dimensions
• Example:
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
thisarray

# An array with more than one dimension


multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray

In the above example, The first and second number in the bracket specifies the amount
of rows and columns.
The last number in the bracket specifies how many dimensions we want
24-01-2025 Dr. V. Srilakshmi
Arrays
• You can access the array elements by referring to the index position. You can
use the [] brackets to access the desired elements from an array.
• Syntax: array[row position, column position, matrix level]
• Example:
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))

multiarray[2, 3, 2]
In the above example, we are trying to access Dim→1
the element of multiarray located in
Row-2
Col-3
Dimention-2
Dim→2
24-01-2025 Dr. V. Srilakshmi
Arrays
• You can also access the whole row or column from a matrix in an array, by
using the c() function.
• Syntax: array[row position, column position, matrix level]
• Example:
thisarray <- c(1:24)

# Access all the items from the first row from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[c(1),,1] #output: 1 5 9

# Access all the items from the first column from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,c(1),2] #output: 13 14 15 16
24-01-2025 Dr. V. Srilakshmi
Data Frames
• Data Frames are data displayed in a format as a table.
• Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However,
each column should have the same type of data.
• Example
• # Create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Print the data frame
Data_Frame

24-01-2025 Dr. V. Srilakshmi


Data Frames
• Use the summary() function to summarize the data from a Data Frame.
• Example2:
# Create a data frame
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)

# Print the data frame


Student

summary(Student)
24-01-2025 Dr. V. Srilakshmi
Data Frames
• We can use single brackets [ ], double brackets [[ ]] or $ to access columns
from a data frame
• Example:
# Accessing a data frame
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)

# Print the data frame


Student[1]
Student[["Name"]]
Student$Name
24-01-2025 Dr. V. Srilakshmi
• To subset a data frame single brackets are used.
• Suppose if f is a data frame.
• f[2] #prints the second column
• f[2,3] #prints second row third column
• f[3, ] #prints all the elements in 3rd row
• f[2:4, ] #prints all the rows from 2nd to 4th
• f[c(2,4), ] #prints 2nd and 4th rows
• You can use negative indexing to remove rows or
columns.

24-01-2025 Dr. V. Srilakshmi


Naming data frame:
• rownames() and colnames() are two functions used to print row and column
names of a data frame and the same are used to assign new row and column
names.
• rownames(f) #prints row names of f
• rownames(f)<-c(“one”,”two”,”three”) # assigns row names
• colnames(f) #prints column names of f
• colnames(f)<-c(“sno”,”sname”,”sage”,”DOB”) # assigns column names

• To set row and column names back to generic index


• rownames(f)=NULL
• colnames(f)=NULL

24-01-2025 Dr. V. Srilakshmi


Data Frames
• Use the rbind() function to add new rows in a Data Frame
• Use the cbind() function to add new column in a Data Frame
• Example:
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
# Adding a row in a data frame
Student2 <- rbind(Student, c("Peter", 25, 5))
Student2
# Adding a column in a data frame
Student3 <- cbind(Student,percentage = c(90.5,81.33,95.43,70.57)
Student3
24-01-2025 Dr. V. Srilakshmi
Data Frames
• Use dim() function to find the number of rows and columns in a Data Frame.
• Use c() function with Negative index to remove rows and columns in a Data
Frame.
• Example:
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
# Removing a row and a column in a data frame
dim(Student)

# Removing a row and a column in a data frame


Student2 <- Student[-c(1),-c(1)]
Student2
24-01-2025 Dr. V. Srilakshmi
Data Frames
• You can also use the ncol() function to find the number of columns and
nrow() to find the number of rows.
• Use the length() function to find the number of columns in a Data Frame
(similar to ncol())
• Example:
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
# finding number of rows and columns
ncol(Student) #3
nrow(Student) #3
length(Student) #3
24-01-2025 Dr. V. Srilakshmi
Data Frames
• Use the rbind() function to combine two or more data frames in R vertically.
• Use the cbind() function to combine two or more data frames in R
horizontally.
• Example:
Student1 <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
Student2 <- data.frame (
Name = c("John1", "Anne1", "Joyce1","Kavitha1"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
Student3 <- rbind(Student1, Student2)
Student3
24-01-2025 Dr. V. Srilakshmi
• Another two functions head() and tail() are used to print the first and last 6
rows of a data frame.
• Syntax:
• head(data frame name,n=6)
• tail(data frame name,n=6)
where n is number of rows. By default it is 6.

• head(f) #prints first 6 rows of data frame f


• head(f,n=2) #prints first 2 rows of data frame f
• tail(f) #prints last 6 rows of data frame f
• tail(f,n=3) #prints last 3 rows of data frame f
24-01-2025 Dr. V. Srilakshmi
Sorting data frames
• Ascending order sorting
sort(f$age) #only age is printed in ascending order
sort(f$age,decreasing=TRUE) #only age is printed in ascending order

To change the ordering of the rows in the data frame we use order()
function
f[order(f$age), ] #all elements in the data frame are printed ordered
by age
f[order(f$age), decreasing=TRUE ]
24-01-2025 Dr. V. Srilakshmi
Examples:
#R Program to demonstrate the use of function
>subtract_two_nums <- function(x, y)
{
return (x – y)
}
> print(subtract_two_nums(3, 1))
• Output:
[1] 2

24-01-2025 Dr. V. Srilakshmi


Examples:
#R Program to demonstrate the use of function
>circumference<- function(r)
{
return 2*pi*r
}
> print(circumference(5))
• Output:
[1] 31.41593

24-01-2025 Dr. V. Srilakshmi


Examples:
#R Program to demonstrate the use of nested functions
radius_from_diameter <- function(d)
{
d/2
}
circumference <- function(r)
{
2*pi*r
}
print(circumference(radius_from_diameter(4)))
Output:
[1] 12.56637
24-01-2025 Dr. V. Srilakshmi
Examples:
#R Program to demonstrate the use of nested functions
sum_circle_ares <- function(r1, r2, r3)
{
circle_area <- function(r)
{
pi*r^2
}
circle_area(r1) + circle_area(r2) + circle_area(r3)
}
print(sum_circle_ares(1, 2, 3))
Output:
[1] 43.9823
24-01-2025 Dr. V. Srilakshmi
Examples:
Lazy Evaluation
R functions perform “lazy” evaluation in which arguments are only evaluated if required
in the body of the function.
# the y argument is not used so not included it causes no harm
lazy <- function(x, y){
x*2 Output: [1] 8
}
lazy(4)
# however, if both arguments are required in the body an error will result if an argument
is missing
lazy2 <- function(x, y){
(x+y)*2
}
lazy2(4)
Output: #Error in lazy2(4): argument "y" is missing, with no default
24-01-2025 Dr. V. Srilakshmi
Examples:
#R Program to demonstrate the use of functions that returns multiple values
bad <- function(x, y)
{
2*x + y
x + 2*y
2*x + 2*y
x/y
}
bad(1, 2)
Output:
[1] 0.5

24-01-2025 Dr. V. Srilakshmi


Examples:
#R Program to demonstrate the use of functions that returns multiple values
good <- function(x, y)
{
output1 <- 2*x + y
output2 <- x + 2*y
output3 <- 2*x + 2*y
output4 <- x/y
c(output1, output2, output3, output4)
}
good(1, 2)
Output:
[1] 4.0 5.0 6.0 0.5
24-01-2025 Dr. V. Srilakshmi
Examples:
#R Program to demonstrate the use of functions with default argument
# Create a function with arguments.
new.function <- function(a = 3, b = 6)
{
result <- a * b
print(result)
}
# Call the function without giving any argument.
new.function()
# Call the function with giving new values of the argument.
new.function(9,5)
Output:
18 45
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi
24-01-2025 Dr. V. Srilakshmi

You might also like