0% found this document useful (0 votes)
18 views27 pages

8 R Basics 3

Uploaded by

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

8 R Basics 3

Uploaded by

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

U18AII5202 – EXPLORATORY DATA

ANALYSIS AND VISUALIZATION

UNIT I – ANALYSIS TECHNIQUES


TOPIC: INTRODUCTION TO STATISTICAL
LEARNING AND R-PROGRAMMING
Matrix
Example 1: To modify a matrix
# create 2 by 2 matrix
matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)

# print original matrix


print(matrix1)

# change value at 1st row, 2nd column to 5


matrix1[1,2] = 5

# print updated matrix


print(matrix1)
Matrix
Example 2: To combine two matrices

# create two 2 by 2 matrices


even_numbers <- matrix(c(2, 4, 6, 8), nrow = 2, ncol =
2)
odd_numbers <- matrix(c(1, 3, 5, 7), nrow = 2, ncol = 2)

# combine two matrices by column


total1 <- cbind(even_numbers, odd_numbers)
print(total1)

# combine two matrices by row


total2 <- rbind(even_numbers, odd_numbers)
print(total2)
Matrix
Example 3: Check if Element Exists in R Matrix

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"),


nrow = 2, ncol = 2)

"Larry" %in% matrix1 # TRUE

"Kinsley" %in% matrix1 # FALSE


List
Example 1: Access list elements

list1 <- list(24, "Sabby", 5.4, "Nepal")

# access 1st item


print(list1[1]) # 24

# access 4th item


print(list1[4]) # Nepal
List
Example 2: Add/remove list
elements

list1 <- list(24, "Sabby", 5.4,


"Nepal")

# change element at index 2


list1[2] <- "Cathy"

# print updated list


print(list1)

# using append() function


append(list1, 3.14)

# remove 4th item


print(list1[-4])
List
Example 3: Check list elements

list1 <- list(24, "Sabby", 5.4, "Nepal")

"Sabby" %in% list1 # TRUE

"Kinsley" %in% list1 # FALSE


List
Example 4: Create list using vectors

empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)

Example 5: Creating a named list


my_named_list <- list(name = "Sudheer", age = 25, city
= "Delhi")

# Printing the named list


print(my_named_list)
List
Example 6: Accessing R List Components

empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)

# Accessing components by names


cat("Accessing name components using $ command\n")
print(empList$Names)
List
Example 7: Accessing R List Components

empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
print(empList[[2]])
print(empList[[2]][2])
print(empList[[1]][4])
List
Example 8: Modifying Components of a List

# Creating a list by naming all its components


empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)

empList$`Total Staff` = 5

empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
print(empList)
List
Example 9: Concatenation of lists Example 10: Converting List to
Vector
# Create two lists.
lst1 <- list(1,2,3) # Create lists.
lst2 <- list("Sun","Mon","Tue") lst <- list(1:5)
print(lst)
# Merge the two lists.
new_list <- c(lst1,lst2) # Convert the lists to vectors.
vec <- unlist(lst)
# Print the merged list.
print(new_list) print(vec)
List
Example 11: Converting list to matrix

# Defining list
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))

# Convert list to matrix


mat <- matrix(unlist(lst1), nrow = 2, byrow =
TRUE)

# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")
Dataframes
Example 1: Creating a dataframe

# creating a data frame


friend.data <- data.frame(
friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
# print the data frame
print(friend.data)
Dataframes
Example 2: Structure of a dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
# using str()
print(str(friend.data))
Dataframes
Example 3: Summary of a dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
# using summary()
print(summary(friend.data))
Dataframes
Example 4: Extracting data from a dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)

# Extracting friend_name column


result <- data.frame(friend.data$friend_name)
print(result)
Dataframes
Example 5: Expanding data from a dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)

# Expanding data frame


friend.data$location <- c("Kolkata", "Delhi",
"Bangalore", "Hyderabad",
"Chennai")
resultant <- friend.data
# print the modified data frame
print(resultant)
Dataframes
Example 6: Accessing elements from a dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)

# Access Items using []


friend.data[1]

# Access Items using [[]]


friend.data[['friend_name']]

# Access Items using $


friend.data$friend_id
Dataframes
Example 7: Number of rows and columns in a
dataframe

friend.data <- data.frame(


friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)

# find out the number of rows and clumns


dim(friend.data)
Dataframes
Example 8: Add Rows in R Data Frame

# Creating a dataframe representing products in a


store
Products <- data.frame(
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)

New_Product <- c(104, "Sunglasses", 39.99, 40)


Products <- rbind(Products, New_Product)

print(Products)
Dataframes
Example 9: Add Columns in R Data Frame

# Existing dataframe representing products in a store


Products <- data.frame(
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)
Discount <- c(5, 10, 8)
Products <- cbind(Products, Discount)

# Rename the added column


colnames(Products)[ncol(Products)] <- "Discount"
print(Products)
Dataframes
Example 10: Remove Rows in R Data Frame

library(dplyr)
# Create a data frame
data <- data.frame(
friend_id = c(1, 2, 3, 4, 5),
friend_name = c("Sachin", "Sourav", "Dravid",
"Sehwag", "Dhoni"),
location = c("Kolkata", "Delhi", "Bangalore",
"Hyderabad", "Chennai")
)

# Remove a row with friend_id = 3


data <- subset(data, friend_id != 3)

data
Dataframes
Example 11: Remove Columns in R Data Frame

library(dplyr)
# Create a data frame
data <- data.frame(
friend_id = c(1, 2, 3, 4, 5),
friend_name = c("Sachin", "Sourav", "Dravid",
"Sehwag", "Dhoni"),
location = c("Kolkata", "Delhi", "Bangalore",
"Hyderabad", "Chennai")
)
data

# Remove the 'location' column


data <- select(data, -location)

data
Dataframes
Example 12: Combine Data frame Vertically

# Creating two sample dataframes


df1 <- data.frame(
Name = c("Alice", "Bob"),
Age = c(25, 30),
Score = c(80, 75)
)
df2 <- data.frame(
Name = c("Charlie", "David"),
Age = c(28, 35),
Score = c(90, 85)
)
# Combining the dataframes using rbind()
combined_df <- rbind(df1, df2)

print(combined_df)
Dataframes
Example 13: Combine Data frame Horizontally

df1 <- data.frame(


Name = c("Alice", "Bob"),
Age = c(25, 30),
Score = c(80, 75)
)
df2 <- data.frame(
Height = c(160, 175),
Weight = c(55, 70)
)

# Combining the dataframes using cbind()


combined_df <- cbind(df1, df2)

print(combined_df)

You might also like