0% found this document useful (0 votes)
9 views19 pages

R Programming Lab

Uploaded by

AJAYKUMAR
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)
9 views19 pages

R Programming Lab

Uploaded by

AJAYKUMAR
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

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 land.

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 R environment. 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 Applications
Course 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

1. Write a R program for different types of data structures in R.


2. Write a R program that include variables, constants, data types.
3. Write a R program that include different operators, control structures, default values for
arguments, returning complex objects.
4. Write a R program for quick sort implementation, binary search tree.
5. Write a R program for calculating cumulative sums, and products minima maxima and
calculus.
6. Write a R program for finding stationary distribution of markanov chains.
7. Write a R program that include linear algebra operations on vectors and matrices.
8. Write a R program for any visual representation of an object with creating graphs
usinggraphic functions: Plot (), Hist (), Linechart (), Pie (), Boxplot (), Scatterplots ().
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.
10. Write a program to create an any application of Linear Regression in multivariate
context forpredictive purpose.
R PROGRAMMING LAB

1. Write a R program for different types of datastructures in R.


Vector

# Creating a character vector


character_vector <- c ("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]


[1,] 1 3 5
[2,] 2 4 6
Lists
# Creating a list
my_list <- list (name = c("John","Daniel","Jack"), age = c (30,53,40), hobbies =c ("reading",
"golf","Gaming"))

my_list

output: $name:[1] "John" "Daniel" "Jack"


$age:[1] 30 53 40
$hobbies:[1] "reading" "golf" "Gaming"
DataFrame

# Creating a data frame


data_frame <- data.frame (Name = c ("Alice", "Bennett", "Charlie"),Age = c (25, 30, 22),
Gender = c ("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 P a g e 1 | 17


R PROGRAMMING LAB

Factors

# Creating a factor
gender <- c ("Male","Female","Male","Female","Male")
factor_gender <- factor (gender, levels = c ("Male","Female"))

factor_gender

output: [1] Male Female Male Female Male


Levels: Male Female

Array

#Creating an Array
arr <- array (1:24, dim = c (4,3,2))
arr

output: , , 1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

,,2
[,1] [,2] [,3]
[1,] 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 P a g e 2 | 17


R 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
PI <- 3.14159265359
paste ("Constant Value:",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)) output: [1] "character"

print(class(age)) output: [1] "integer"

print(class(is_student)) output: [1] "logical"

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 3 | 17


R PROGRAMMING LAB

3.Write a R program that include different operators, control structures, default


values for arguments, returning complex objects
# Arithmetic operators
a<-11
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 < b) {
print <- "a is less than b"
} else {
print <- "a is equal to b"
}
output:[1] "a is greater than b"

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 4 | 17


R PROGRAMMING LAB

# Default values for arguments

my_function <- function (country = "INDIA") {


paste("I am from", country)
}
my_function("USA")
my_function () # will get the default value, which is INDIA

output: [1] "I am from USA"


[1] "I am from INDIA"

# Returning complex objects

res<-function () {
v<-c (1,2,5,3,8)
m<-matrix (1:8, ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list (vec=v1, mat=m1)
return(L)
}
res ()
output: $vec
[1] 3.8

$mat
[1] 1

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 5 | 17


R PROGRAMMING LAB

4.Write a R program for quick sort implementation, binary search tree Quick Sort

# Quick Sort Function

quick_sort <- function(arr) {

if (length(arr) <= 1) {

return(arr) # Base case: array is already sorted

pivot <- arr[1] # Choose the first element as pivot


left <- arr[arr < pivot] # Elements less than pivot
right <- arr[arr > pivot] # Elements greater than pivot

return(c(quick_sort(left), pivot, quick_sort(right)))


}

# Example usage of Quick Sort

arr <- c (2,5,3,6,8,4,1,3,10)


cat("Original Array:\n", arr, "\n")

sorted_arr <- quick_sort(arr)


cat("Sorted Array (Quick Sort):\n", sorted_arr, "\n")

output: [1] 1 2 3 3 4 5 6 8 10

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 6 | 17


R PROGRAMMING LAB

# Define the structure for a Binary Search Tree node

bst_node <- function(key) {


return (list (key = key, left = NULL,right = NULL))
}

# Function to insert a key into the BST

insert <- function (root, key) {


if (is.null(root)) {
return(bst_node(key))
}
if (key < root$key) {
root$left <- insert(root$left, key)
} else if (key > root$key) {
root$right <- insert(root$right, key)
}
return(root)
}

# Function to perform an in-order traversal of the BST

in_order_traversal <- function(root) {


if (!is.null(root)) {
in_order_traversal(root$left)
cat(root$key, " ")
in_order_traversal(root$right)
}
}

# Example usage:

bst <- NULL


keys <- c (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:1 2 3 5 8 9

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 7 | 17


R PROGRAMMING LAB

5.Write a R program for calculating cumulative sums, and products minima


maxima and calculus
# Sample vector of numbers
numbers <- c (1, 2, 3, 4, 5)

# Calculate cumulative sum


cumulative_sum <- cumsum(numbers)
cat ("Cumulative Sum:", cumulative_sum, "\n")

output: Cumulative Sum: 1 3 6 10 15

# 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, e.g., f(x) = x^2

f <- function(x) x^2

# Calculate the derivative of the function

derivative <- Deriv(f)


cat ("Derivative of f(x) = x^2:", derivative (2), "\n") # Evaluate the derivativeat x = 2

output: Derivative of f(x) = x^2: 4

# Integrate the function from 1 to 5

integral <- integrate (f, lower = 1, upper = 5)


cat ("Integral of f(x) = x^2 from 1 to 5:", integral$value, "\n")

output: Integral of f(x) = x^2 from 1 to 5: 41.33333

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 8 | 17


R PROGRAMMING LAB

6.Write a R program for finding stationary distribution of markanov chains

# Load the markovchain package


library(markovchain)

# Define the transition matrix for your Markov chain

transition_matrix <- matrix (c (0.8, 0.2,0.4, 0.6), nrow = 2, byrow =TRUE)

# Define the states

states <- c ("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.6666667 0.3333333

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 9 | 17


R PROGRAMMING LAB

7.Write a R program that include linear algebra operations on vectors & matrices

# Create a Two Vectors

v1 <- c(2, 4, 6,8)

v2 <- c(1, 3, 5,7)

# Dot product (inner product) of two vectors

dot_product <- sum(v1 * v2)

cat("Dot Product of Vectors:\n", dot_product,"\n\n")

output: Dot Product of Vectors: 100

# Scalar multiplication with a vector

scalar <- 3
scalar_multiply_vector <- scalar * v1
cat("Scalar Multiplication with Vector:\n", scalar_multiply_vector, "\n\n")

output: Scalar Multiplication with Vector: 6 12 18 24

# Create a square matrix

matrix_A <- matrix (1:4, nrow = 2)

# Matrix determinant (for square matrices)

determinant_A <- det(matrix_A)


cat ("Determinant of Matrix A:", determinant_A, "\n")

output: Determinant of Matrix A: -2

# Matrix inverse (for square matrices)

inverse_A <- solve(matrix_A)


cat("Inverse of Matrix A:\n")
print(inverse_A)

output: Inverse of Matrix A:


[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 10 | 17


R PROGRAMMING LAB

8.Write a R program for any visual representation of an object with creating graphs
using graphic functions: Plot (), Hist (), Linechart (), Pie (), Boxplot (), Scatterplots()

# Create a sample data set

data <- c(3, 4, 7, 8, 9, 10, 12, 14, 15, 18, 21)

# Plot

plot(data)

# Create a histogram

hist(data, breaks = 5, main = "Histogram", xlab = "Value", ylab = "Frequency", col = "green")

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 11 | 17


R PROGRAMMING LAB

# Create a line chart

x <- 1:length(data)
line_data <- cumsum(data)
plot(x, line_data, type = "l", col = "red", main = "Line Chart", xlab = "X-axis", ylab = "Y-axis")

# Create a pie chart

slices <- c (30, 20, 10, 40)


lbls <- c ("Slice 1", "Slice 2", "Slice 3", "Slice 4")
pie (slices, labels = lbls, main = "Pie Chart")

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 12 | 17


R PROGRAMMING LAB

# Create a boxplot

boxplot (data, main = "Boxplot", xlab = "Value", ylab = "Distribution", col = "purple")

# Create a scatterplot

x2 <- c(2, 4, 6, 8, 10)


y2 <- c(5, 7, 3, 9, 2)
plot(x2, y2, type = "p", pch = 20, col = "orange", main = "Scatterplot", xlab = "X-axis", ylab = "Y-axis")

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 13 | 17


R 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 = c ("Female", "Male", "Male", "Male", "Female"),
Score = c (85, 92, 78, 88, 95))

# Indexing and Subsetting

cat("\nSubset of Data Frame (Age > 25):\n")


subset_data <- data_frame[data_frame$Age > 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_frame$Score)


summary_stats

output: Min. 1st Qu. Median Mean 3rd Qu. Max.


78.0 85.0 88.0 87.6 92.0 95.0

# Add a new column

data_frame$Grade <- ifelse(data_frame$Score >= 90, "A", ifelse(data_frame$Score >= 80, "B", "C"))
print(data_frame)

output: Name Age Gender Score Grade


1 Alice 25 Female 85 B
2 Bennete 30 Male 92 A
3 Charlie 22 Male 78 C
4 David 28 Male 88 B
5 Emma 35 Female 95 A

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 14 | 17


R PROGRAMMING LAB

# Grouping and Aggregation

gender_avg_score <- aggregate (data_frame$Score, by = list(data_frame$Gender), FUN = mean)


colnames(gender_avg_score) <- c("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 P a g e 15 | 17


R PROGRAMMING LAB

10.Write a program to create an any application of Linear Regression in


multivariate context for predictive purpose.

# Load the mtcars dataset


data(mtcars)

# Explore the dataset


head(mtcars)
output:

# Fit a multivariate linear regression model


# We'll predict 'mpg' (miles per gallon) based on 'hp' (horsepower) and 'wt' (weight)
model <- lm(mpg ~ hp + wt, data = mtcars)
model

lm(formula = mpg ~ hp + wt, data = mtcars)

# Print the model summary

Suhas B Raj, Asst.Prof. Dept of CA, MIT FGC, Mysore P a g e 16 | 17


R PROGRAMMING LAB

summary(model)

# Make predictions using the model


new_data <- data.frame(hp = c(150, 200), wt = c(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 P a g e 17 | 17

You might also like