0 ratings 0% found this document useful (0 votes) 22 views 19 pages EMERGENCY
The document outlines the vision and mission of the Maharaja Education Trust's MIT First Grade College, emphasizing educational excellence and social responsibility. It provides a detailed syllabus for an R Programming Lab course, including learning objectives, course outcomes, and a list of programming experiments focused on statistical analysis and data manipulation using R. The document also includes example R code for various programming tasks related to data structures, statistical techniques, and visualizations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Maharaja Education Trust ®
MIT First Grade College MEI
Department of Computer Applications !Y!5=8
Industrial Suburb, Manandavadi Road, Mysuru -570008, www.mitfgc.in
Affiliated to University of Mysore, Accredited by NAAC with “A” Grade, ISO 9001:2015 Certified Institution
EOUCATION
VISION OF THE INSTITUTE.
Empower the individuals and society at large through educational excellence; sensitize them
‘for a life dedicated to the service of fellow human beings and mother tand.
MISSION OF THE INSTITUTE
To impact holistic education that enables the students to become socially responsive and
useful, with roots firm on traditional and cultural values; and to hone their skills to accept
challenges and respond to opportunities in a global scenario,
Lecture Notes on: R PROGRAMMING LAB
Course Code: BCADSC14-LAB
Contact Hours: 04 hrs per week
Formative Assessment Marks: 25
Summative Exam Marks: 25
Exam Duration: 03hrs
Overview
The following program problematic comprises of R programming basics and application of
several Statistical Techniques using it. The module aims to provide exposure in terms of
Statistical Analysis, Hypothesis Testing, Regression and Correlation using R programming
language.
Learning Objectives
The objective of this Laboratory to make students exercise the fundamentals of statistical
analysis in Renvironment. They would be able to analysis data for the purpose of exploration
using Descriptive and Inferential Statistics. Students will understand Probability and Sampling
Distributions and learn the creative application of Linear Regression in multivariate context for
predictive purpose.
Prepared by: Asst. Prof. Suhas B Raj
Department: Computer ApplicationsCourse Outcomes:
Install, Code and Use R Programming Language in R Studio IDE to perform basic tasks
on Vectors, Matrices and Data frames, Explore fundamentals of statistical analysis in R
environment.
Describe key terminologies, concepts and techniques employed in Statistical Analysis.
Define Calculate, Implement Probability and Probability Distributions to solve a wide
variety of problems.
Conduct and interpret a variety of Hypothesis Tests to aid Decision Making.
Understand, Analyze, and Interpret Correlation Probability and Regression to analyze the
underlying relationships between different variables.
List of Experiments
Write a R program for different types of data structures in R.
Write a R program that include variables, constants, data types.
Write a R program that include different operators, control structures, default values for
arguments, returning complex objects.
Write a R program for quick sort implementation, binary search tree.
Write a R program for calculating cumulative sums, and products minima maxima and
calculus.
Write a R program for finding stationary distribution of markanov chains.
Write a R program that include linear algebra operations on vectors and matrices.
Write a R program for any visual representation of an object with creating graphs
usinggraphic functions: Plot (), Hist (), Linechart (), Pie (), Boxplot (), Scatterplots ().
Write a R program for with any dataset containing data frame objects, indexing and sub
setting data frames, and employ manipulating and analyzing data.
Write a program to create an any application of Linear Regression in multivariate
context forpredictive purpose.1, WriteaR program for different ‘types of datastructuresin R.
Vector
# Creating a character vector
character_vector <- ¢ "apple", “banana”, "cherry")
character_vector
output: [1] “apple” “banana” "cherry"
Matrix
# Creating a numeric matrix
‘numeric_matrix <- matrix (1:6, nrow = 2, ncol = 3)
numeric_matrix
output: [.1] [2] [3]
fra 6s
2] 2 4 6
Lists
# Creating a list
my_list< list (name = e("John","Daniel","Jack"), age = ¢ (30,$3,40), hobbies =c (“reading”,
“golf”,"Gaming"))
my,
output: Sname{1] “John” "Daniel" "Jack"
Sager[1] 30.53 40
Shobbies:{1] "reading" “golf” "Gaming"
DataFrame
# Creating a data frame
data_frame <- data frame (Name ~ c ("Alice”, "Bennett", "Charlie"),Age = ¢ (25, 30, 22),
Gender = ¢ ("Female", "Male", "Male"))
data_frame
output: Name Age Gender
1 Alice 25 Female
2 Bennett 30. Male
3 Charlie 22 Male
‘Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore Page 1[17R PROGRAMMING LAB
Factors
# Creating a factor
gender <- ¢ ("Male”,"Female’,”Male”,"Female”,"Male")
factor_gender <- factor (gender, levels = ¢ ("Male","Female"))
factor_gender
output: [1] Male Female Male Female Male
Levels: Male Female
Array
Creating an Array
arr < array (1:24, dim = ¢ (4,3,2))
art
(L263)
wis 9
2) 2 6
Bl3a7n
[4] 48
eit
(AV L2) 13)
{L] 13°17 21
2) 14 18 22
(3) 15 19 23
{4) 16 20 24
‘Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 2\17R PROGRAMMING LAB
2.Write a R program that include variables, constants, data types.
# Define variables
radius <-5
radius
output:{1] 5
name <- "Alice"
name
output:[1] "Alice"
age <- 30L
age
output-{1] 30
is_student <- TRUE
is_student
output: [1] TRUE
# Constants
PL-<- 3.14159265359
paste ("Constant Valu
PI)
output:[1] "Constant Value: 3 14159265359"
GREETING <- "Hello, World!"
paste ("Constant Value:", GREETNG)
output:[1] "Constant Value: Hello, World!"
# Data types
print(class(radius)) output: [1] "numeric"
print(class(name)) ourput: [1] "character
print(class(age)) output: [1] "integer"
print(class(is_student)) output: [1] "logical”
‘Suhas B Rej, Asst.Prof, Dept of CA, MIT FGC, Mysore Page 3]17R PROGRAMMING LAB
3.Write a R program that include different operators, control structures, default
values for arguments, returning complex objects
# Arithmetic operators
asl
b<4
‘sum_result <- a+b
sum_result
output:[1] 15
diff_result< a-b
diff_result
output:[1] 7
product_result<- a*b
product_result
output;[1] 44
division_result <- a/b
division_result
output;[1] 2.75
modulus_result<-a%%b
modulus_result
output:(1] 3
# Control structure (if-else)
if(a>b) {
print ("a is greater than b")
} else if(a pivot]
retum(c(quick_sori(left), middle, quick_sort(right)))
i;
veet = © (2,5,3,6,8,441,3,10)
print ("Unsorted Vector")
print(veet)
output: [1] 2536841310
sorted_vector <- quick_sort(veet)
prim("sorted vector")
Print(sorted_vector)
ouput: 1] 1233456810
‘Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore Page 17R PROGRAMMING LAB
4 Define the structure for a Binary Search Tree node
bst_node < function(key) {
return (list (key = key, left = NULL «ight = NULL))
$
# Function to insert a key into the BST
insert <- function (root, key) {
if Gis.null(root)) {
return(bst_node(key))
4
if (key rootSkey) {
rootSright <- insert(rootSright, key)
4
return(root)
$
# Function to perform an in-order traversal of the BST
in_order_traversal <- function(root) {
if (is.null(root)) {
in_order_traversal(rootSleft)
cat(rootSkey,"")
in_order_traversal(rootSright)
$
# Example usage:
bst << NULL.
keys < © (5, 3,8, 1,9,2)
for (key in keys) {
bst < insert (bst, key)
}
cat ("In-order traversal of BST:", *\n")
‘in_order_traversal(bst)
output:| 23589
Suhas B Raj, Asst Prof, Dept of CA, MIT FGC, Mysore Page 7117R PROGRAMMING LAB
5.Write a R program for calculating cumulative sums, and products minima
maxima and calculus
# Sample vector of numbers
numbers <-¢ (1, 2,3,4, 5)
# Calculate cumulative sum
cumulative_sum <- cumsum(numbers)
cat ("Cumulative Sum:", cumulative_sum, "\n")
output: Cumulative Sum: | 3 6 1015,
# Calculate cumulative product
cumulative_product <- cumprod(numbers)
cat("Cumulative Product:”, cumulative_product, "\n")
output: Cumulative Product: 1 2 6 24 120
# Calculate minimum and maximum
min_value <- min(numbers)
max_value <- max(numbers)
cat ("Minimum:", min_value,"\n")
cat ("Maximum:", max_value,"\n")
output: Minimum:1 — Maximum:5
library (Deriv) # Basic calculus operations
# Define a function, ¢.g., ffx) =x°2
f< function(x) x*2
# Calculate the derivative of the function
derivative <- Deriv(f)
cat ("Derivative of fx) = x“2:", derivative (2), "\n") # Evaluate the derivativeat x = 2
output: Derivative of f(x) = x"2: 4
# Integrate the function from 1 10 5
integral <- integrate (f, lower = 1, upper = 5)
cat ("Integral of f(x) = x2 from 1 to 5:", integralSvalue, "\n")
output: Integral of f(x) = x2 from I to S: 41.3333
Suhas B Raj, Asst Prof, Dept of CA, MIT FGC, Mysore Page 8|17UB Solo Trina ce AN}
6.Write a R program for finding stationary distribution of markanov chains
# Load the markovchain package
library(markovchain)
# Define the wansition matrix for your Markov chain
transition_matrix <- matrix (¢ (0.8, 0.2,0.4, 0.6), nrow = 2, byrow
# Define the states
states <-¢ ("State A", "State B")
# Create a Markov chain object
my_markov_chain <- new ("markovchain”, states = states, transitionMatrix ~transition_matrix)
# Find the stationary distribution
stationary_dist <. steadyStates(my_markov_chain)
# Print the stationary distribution
cat ("Stationary Distribution:")
print(stationary dist)
Output: Stationary Distribution:
State A State B
[1,] 0.666667 0.333333
‘Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 9|17R PROGRAMMING LAB
7.Write a R program that include lincar algebra operations on vectors & matrices
# Create two square matrices
matrix_A <- matrix (1
matrix_B < matrix (5:
# Matrix determinant (for square matrices)
determinant_A <- det(matrix_A)
cat (“Determinant of Matrix A:", determinant_A, "\n")
output: Determinant of Matrix A: -2
4 Matrix inverse (for square matrices)
inverse_A <- solve(matrix_A)
cat("Inverse of Matrix A:\n")
print(inversc_A)
output: Inverse of Matrix A:
(1) 02)
(ly -2 15
(2) 1-05
‘Suhas B Raj, Asst.Prof, Dept of CA, MIT FGC, Mysore Page 1017R PROGRAMMING LAB
8,Write a R program for any visual representation of an object with creating graphs
using graphic functions: Plot 0), Hist (), Linechart (), Pie (), Boxplot (), Scatterplots()
# Create a sample data set
data <- (3, 4, 7, 8,9, 10, 12, 14, 15, 18, 21)
# Plot 2 :
plot(data) °
15
10
# Create a histogram
hist(data, breaks = 5, main = "Histogram", xlab
alue”, ylab = "Frequency", col = "green")
Histogram
Frequency
2
‘Suhas B Raj, Asst.Prof, Dept of CA, MIT FGC, Mysore Page 17R PROGRAMMING LAB
# Create a line chart
x < T:length(data)
line_data <- cumsum(data)
plot(x, line_data, type = "I", col
‘red", main = "Line Chart", xlab = "X-axis", ylal
Line Chart
Yoaxis
6
# Create a pie chart
slices <- ¢ (30, 20, 10, 40)
Ibls <-¢("Slice 1", "Slice 2", “Slice 3", "Slice 4")
pie (slices, labels ~ Ibs, main = "Pie Chart")
‘Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore Page 12|17R PROGRAMMING LAB
# Create a boxplot
"Value", ylab = "Distribution", col = “purple")
Boxpiot
boxplot (data, main = "Boxplot", xlab
# Create a scatterplot
x2.< (2, 4,6, 8, 10)
y2< (5, 73,92)
plot(x2, y2, type = "p", pch = 20, col = "orange", main = "Scatterplot", xlab ="X-axis", ylab="Y-axis")
neoearptot
8 7
i :
° :
Suhas B Raj, AsstProf. Dept of CA, MITT FGC, Mysore Page 13|17R PROGRAMMING LAB
9.Write a R program for with any dataset containing data frame objects, indexing
and sub setting data frames, and employ manipulating and analyzing data
# Create a sample data frame
data_frame <- data, frame(Name = c ("Alice", "Bennett", "Charlie", "David", "Emma"),
Age =c (25, 30, 22, 28, 35),
Gender = ¢ ("Female", "Male", “Male”, "Male", "Female"),
Score = ¢ (85, 92, 78, 88, 95))
# Indexing and Subsetting
cat("\nSubset of Data Frame (Age > 25):\n")
subset_data <- data_frame{data_frameSAge > 25, ]
print(subset_data)
output: Name Age Gender Score
2 Bennett 30 Male 92
4 David 28 Male 88
5 Emma 35 Female 95
# Calculate Summary Statistics
summary_stats <= summary(data_frameSScore)
summary_stats
output: Min. Ist Qu. Median Mean 3rd Qu. Max.
78.0 85.0 880 876 920 950
# Add a new column
data_frameSGrade = 90, "A", ifelse(data_frameSScore >= 80,"B", "C"))
output: Name Age Gender Score Grade
1 Alice 25 Female 85
Bennete 30 Male 92
‘Charlie 22 Male 78
David 28 Male 88
Emma 33 Female 9S
>eore
Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 1}17R PROGRAMMING LAB
# Grouping and Aggregation
gender_avg_score < aggregate (data_frame$Score, by = list(data_frameSGender), FUN =mean)
colnames(gender_avg_score) <- ¢("Gender", "Avg Score")
Print(gender_avg_score)
output: Gender Avg Score
1 Female 90
2° Male 86
‘Suhas B Raj, Asst Prof, Dept of CA, MIT FGC, Mysore Powe 18|17R PROGRAMMING LAB
10, Write a program to create an any application of Linear Regression in
multivariate context for predictive purpose.
# Load the micars dataset
data(mtcars)
# Explore the dataset
head(mtcars)
output:
mpg cyl disp hp drat we qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
Mazda RX4 Wag 21.0 6-160 110 3.80 2.875 17.02
Datsun 710 22.8 4 108 93 3.85 2.320 18.61
Hornet 4 Drive 21.4 6 258 110 3,08 3,215 19.44
8
6
Hornet Sportabout 18. 360 175 3.15 3.440 17.02
Valiant 18.1 225 105 2.76 3.460 20.22
ooore
# Fit a multivariate linear regression model
# We'll predict ‘mpg' (miles per gallon) based on ‘hp! (horsepower) and ‘wt veight)
model < Im(mpg ~ hp + wt, data = mtcars)
model
call:
Im(formula = mpg ~ hp + wt, data = mtcars)
Coefficients:
(intercept) ap we
37.22727 -0,03177— 3.87783
Im(formula = mpg ~ hp + wt, data = mtears)
cana:
im(formula = mpg ~ hp + wt, data = mecars)
Coefticiencs:
(Intercept) bp we
37.22727 © -=0.03177 3.87783
Suhas Raj, Ass.Prof. Dept of CA, MIT FGC, Mysore Page 1617R PROGRAMMI)
# Print the model summary
‘summary(model)
Cail:
im(formula = mpg ~ mp + we, data = mtcars)
Residual:
Min 10 Median 300 Max
3.541 -1.600 -0.182 1.080 S.e54
Coefficients:
Estimate Std. Error ¢ value Pr(>ItI)
(Intercept) 37.22727 1.89879 23.285 < 2e-16 **
np ~0.03177 0.00903 -3.518 0.00145 **
we -3.87783 0.63273 -6.129 1.12e-06 «+
Signif. codes: 0 ‘+
0.002 **** 0.01 *** 9.05
Ore a
Residual standard error: 2.593 on 29 degrees of freedom
Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
E-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12
# Make predictions using the model
new_data <- data.frame(hp = ¢(150, 200), wt = ¢(3.5, 4.0)
predictions <- predict(model, newdata = new_data)
cat("Predicted MPG for new data:\n")
print(predictions)
‘output: Predicted MPG for new data
1 2
18.88892 15.36136
Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Pa
©1717