R LAB Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

SMT.

KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &


MANAGEMENT STUDIES

1. Write an R program Illustrate with if-else statement and how does it


operate on vectors of variable length.
2. Write an R program Illustrate with for loop and stop on condition, to print
the error message.
3. Write an R Program To find Factorial of given number using recursion.
4. Write an R Program to implement T-Test for Anova.
5. Write an R Program Compute mean values for vector aggregates defined
by factors tapply and sapply.
6. Write a R program for finding stationary distribution of markanov chains.
7. Write an R Program for implementing Quick Sort for Binary Search.
8. Write an R Program Illustrate Reading & Writing Files.
9. Write a R program for any visual representation of an object with creating
graphs using graphic functions:
Plot(),Hist(),Linechart(),Pie(),Boxplot(),Scatterplots().
10. Write a R program for with any dataset containing data frame objects,
and employ manipulating and analyzing data.
11. Write a program to create an any application of Linear Regression in
multivariate context for predictive purpose.
12. Write an R Program to Find Mean, Mode & Median.
To run the R program I provided in RStudio, you can follow these steps:

1. Open RStudio: Launch RStudio on your computer.


2. Create a New Script: Click on "File" in the top menu bar, then select "New Script" to
create a new R script file.
3. Copy the Code/type the code: Copy/type the below R program code

# Create a vector of numbers with variable length


numbers <- c(2, 7, 10, 15, 8, 3, 6)

# Initialize an empty vector to store the results


results <- vector()

# Use a for loop to iterate through the numbers


for (num in numbers) {
# Check if the number is even or odd
if (num %% 2 == 0) {

BCA- V- R-LAB MANUAL Page 1


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
result <- "Even"
} else {
result <- "Odd"
}

# Append the result to the results vector


results <- c(results, result)
}

# Print the original vector and the results


cat("Original Vector: ", numbers, "\n")
cat("Classification: ", results, "\n")

OUTPUT:

Original Vector: 2 7 10 15 8 3 6
Classification: Even Odd Even Odd Even Odd Even

4. Paste the Code: Paste the copied code into the newly created R script file in RStudio.
5. Run the Code: To run the code, you can either:

BCA- V- R-LAB MANUAL Page 2


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
o Select the entire code with your mouse and click the "Run" button at the top of the
script editor, or
o Use the keyboard shortcut Ctrl+Enter (Windows/Linux) or Command+Enter
(Mac) with the code selected.
6. View the Output: Once you run the code, you should see the output in the RStudio
Console panel at the bottom left of the RStudio window.

The output will be displayed in the Console panel, as explained in my previous response,
showing the original vector and the classification results.

1. Write an R program Illustrate with if-else statement and how does it


operate on vectors of variable length.
# Create a vector of numbers with variable length
numbers <- c(2, 7, 10, 15, 8, 3, 6)

# Initialize an empty vector to store the results


results <- vector()

# Use a for loop to iterate through the numbers


for (num in numbers) {
# Check if the number is even or odd
if (num %% 2 == 0) {
result <- "Even"
} else {
result <- "Odd"
}

# Append the result to the results vector


results <- c(results, result)
}

# Print the original vector and the results


cat("Original Vector: ", numbers, "\n")
cat("Classification: ", results, "\n")

OUTPUT:
Original Vector: 2 7 10 15 8 3 6

Classification: Even Odd Even Odd Even Odd Even

BCA- V- R-LAB MANUAL Page 3


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

2. Write an R program Illustrate with for loop and stop on condition, to


print the error message.
# Set a flag to indicate if an error occurred
error_flag <- FALSE

# Generate random numbers and stop on condition


for (i in 1:10) { # Loop 10 times (you can change this
number)
random_number <- runif(1) # Generate a random number
between 0 and 1

# Check if the random number is greater than 0.9


if (random_number > 0.9) {
error_flag <- TRUE
cat("Error: Random number exceeded the threshold (",
random_number, ")\n")
break # Exit the loop when the condition is met
}

# Perform other operations here if needed

# Check if an error occurred


if (!error_flag) {
cat("No errors encountered.\n")
}

OUTPUT
No errors encountered.

Here's a simple R program that uses a for loop and stops when a certain condition is met. In this
example, the program generates random numbers and stops when a number greater than 0.9 is
encountered, printing an error message.

ERROR ENCOUNTED

If you want the program to print an error message whenever a random number greater than 0.9 is
encountered but still continue running and generating numbers, you can make a slight

BCA- V- R-LAB MANUAL Page 4


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
modification. Instead of using break to exit the loop when the condition is met, you can continue
the loop and print the error message. Here's the modified program:

# Set a flag to indicate if an error occurred


error_flag <- FALSE

# Generate random numbers and print error on condition


for (i in 1:10) { # Loop 10 times (you can change this
number)
random_number <- runif(1) # Generate a random number
between 0 and 1

# Check if the random number is greater than 0.9


if (random_number > 0.9) {
error_flag <- TRUE
cat("Error: Random number exceeded the threshold (",
random_number, ")\n")
}

# Perform other operations here if needed

# Check if an error occurred


if (!error_flag) {
cat("No errors encountered.\n")
}

OUTPUT
Error: Random number exceeded the threshold ( 0.9273901 )
Error: Random number exceeded the threshold ( 0.9296098 )

With this modification, the program will continue running and generating
random numbers even when it encounters a random number greater than 0.9. It
will print an error message for each occurrence of such a number but won't
exit the loop until it completes all iterations. After the loop, it will
still check if an error occurred and print a message accordingly.

BCA- V- R-LAB MANUAL Page 5


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

3. Write an R Program To find Factorial of given number using


recursion.
# Function to calculate factorial using recursion
factorial <- function(n) {
if (n == 0) {
return(1) # Base case: factorial of 0 is 1
} else {
return(n * factorial(n - 1)) # Recursive case
}
}

# Check if a command-line argument is provided


if (length(commandArgs(trailingOnly = TRUE)) > 0) {
# Get the first command-line argument as a character
num_text <- commandArgs(trailingOnly = TRUE)[1]

# Check if the input is a valid non-negative integer


if (grepl("^[0-9]+$", num_text)) {
num <- as.integer(num_text)

if (num >= 0) {
result <- factorial(num)
cat("Factorial of", num, "is", result, "\n")
} else {

BCA- V- R-LAB MANUAL Page 6


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
cat("Factorial is not defined for negative
numbers.\n")
}
} else {
cat("Invalid input. Please enter a non-negative
integer.\n")
}
} else {
cat("Usage: Rscript script_name.R <non-negative-
integer>\n")
}

OUTPUT
F:\R-Program\lab>Rscript factorial.R 5

Factorial of 5 is 120

F:\R-Program\lab>Rscript factorial.R 3

Factorial of 3 is 6

F:\R-Program\lab>Rscript factorial.R 4

Factorial of 4 is 24

F:\R-Program\lab>Rscript factorial.R -2

Invalid input. Please enter a non-negative integer.

Note: To be run as command line arguments

BCA- V- R-LAB MANUAL Page 7


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

4. Write an R Program to implement T-Test for Anova.


R program that implements a t-test for one-way analysis of variance (ANOVA). The program
assumes you have two groups of data, and it will perform an independent samples t-test to
compare the means of the two groups to determine if there is a significant difference between
them:

# Sample data for Group 1


group1 <- c(23, 25, 29, 28, 32)

# Sample data for Group 2


group2 <- c(35, 38, 42, 39, 40)

# Perform independent samples t-test


t_result <- t.test(group1, group2)

# Print the results


cat("T-Test Results:\n")
cat(" - p-value:", t_result$p.value, "\n")
cat(" - t-statistic:", t_result$statistic, "\n")
cat(" - Degrees of Freedom:", t_result$parameter,
"\n")
cat(" - Confidence Interval (95%): [",
t_result$conf.int[1], ",", t_result$conf.int[2], "]\n")

BCA- V- R-LAB MANUAL Page 8


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
cat(" - Test Conclusion:")
if (t_result$p.value < 0.05) {
cat(" Reject the null hypothesis (significant
difference)\n")
} else {
cat(" Fail to reject the null hypothesis (no
significant difference)\n")
}

OUTPUT :
T-Test Results:
> cat(" - p-value:", t_result$p.value, "\n")
- p-value: 0.0005245777
> cat(" - t-statistic:", t_result$statistic, "\n")
- t-statistic: -5.848077
> cat(" - Degrees of Freedom:", t_result$parameter, "\n")
- Degrees of Freedom: 7.360587
> cat(" - Confidence Interval (95%): [", t_result$conf.int[1], ",",
t_result$conf.int[2], "]\n")
- Confidence Interval (95%): [ -15.96412 , -6.835883 ]
> cat(" - Test Conclusion:")
- Test Conclusion:> if (t_result$p.value < 0.05) {
+ cat(" Reject the null hypothesis (significant difference)\n")
+ } else {
+ cat(" Fail to reject the null hypothesis (no significant difference)\n")
+ }
Reject the null hypothesis (significant difference)

BCA- V- R-LAB MANUAL Page 9


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
5. Write an R Program Compute mean values for vector aggregates
defined by factors tapply and sapply.
R program that computes the mean values for vector aggregates defined by factors using the
tapply and sapply functions:

# Sample data: A numeric vector


values <- c(42, 55, 38, 65, 49, 72, 53, 66, 41, 58)

# Factor variable to define groups


groups <- factor(c("A", "B", "A", "B", "A", "B", "A",
"B", "A", "B"))

# Compute mean values for each group using tapply


mean_by_group <- tapply(values, groups, mean)

# Compute mean values for each group using sapply


mean_by_group_sapply <- sapply(unique(groups),
function(group) {
mean(values[groups == group])
})

# Print the results


cat("Mean values by group (tapply):\n")
print(mean_by_group)

cat("\nMean values by group (sapply):\n")


print(mean_by_group_sapply)
OUTPUT
Mean values by group (tapply):
> print(mean_by_group)
A B
44.6 63.2
>
> cat("\nMean values by group (sapply):\n")

Mean values by group (sapply):


> print(mean_by_group_sapply)
[1] 44.6 63.2

BCA- V- R-LAB MANUAL Page 10


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

tapply and sapply:


NOTE:

tapply and sapply are two useful functions in R for working with data, especially when dealing
with grouped data or applying functions to elements of vectors. Here's an explanation of each:

1. tapply:

tapply stands for "table apply." It is used to apply a function to subsets of a vector or
array, split by one or more factors. This is particularly useful when you want to compute
summary statistics or perform operations on data grouped by certain categories.

The basic syntax of tapply is as follows:

tapply(data, INDEX, FUN)

 ‘data: The vector or array you want to split into groups.


 INDEX: A factor or a list of factors that defines the groups.
 FUN: The function to be applied to each group.

tapply returns an array where the values are the results of applying the function FUN to each
group defined by the factors.

Example
BCA- V- R-LAB MANUAL Page 11
SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
# Calculate the mean age for different genders

tapply(ages, genders, mean)

2. sapply:

sapply stands for "simplify apply." It is a more generalized version of lapply and is used to
apply a function to each element of a list or vector. The result is typically simplified into a vector
or array when possible.

The basic syntax of sapply is as follows:

sapply(X, FUN)

 X: The list or vector you want to apply the function to.


 FUN: The function to apply to each element of X.

sapply returns a vector, matrix, or array, depending on the structure of the results.

Example

# Square each element in a vector

sapply(numbers, function(x) x^2)

In summary, tapply is mainly used for applying functions to grouped data defined by factors,
while sapply is used for applying functions to elements of a list or vector. Both functions are
handy for data manipulation and analysis in R.

6. Write a R program for finding stationary distribution of


markanov chains.
R program to find the stationary distribution of a Markov chain without the need for additional
packages:

# Define the transition matrix (replace with your own data)


transition_matrix <- matrix(c(
0.7, 0.3,
0.2, 0.8
), nrow = 2, byrow = TRUE)

# Initial state probabilities (replace with your own data)


BCA- V- R-LAB MANUAL Page 12
SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
initial_probabilities <- c(0.5, 0.5)

# Number of iterations for convergence


num_iterations <- 1000

# Initialize the state vector


current_state <- initial_probabilities

# Perform iterations to find the stationary distribution


for (i in 1:num_iterations) {
current_state <- transition_matrix %*% current_state
}

# Print the stationary distribution


cat("Stationary Distribution:\n")
print(current_state)

OUTPUT

Stationary Distribution:
> print(current_state)
[,1]
[1,] 0.5
[2,] 0.5

BCA- V- R-LAB MANUAL Page 13


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
Stationary Distribution:

[,1]

[1,] 0.625

[2,] 0.375

7. Write an R Program for implementing Quick Sort for Binary Search.


here's a simple R program that implements the Quick Sort algorithm, which is a sorting
algorithm, and then performs a binary search to find a target element within the sorted array:

# Quick Sort function


quick_sort <- function(arr) {
if (length(arr) <= 1) {
return(arr)
}

pivot <- arr[1]


less <- arr[arr < pivot]
equal <- arr[arr == pivot]
greater <- arr[arr > pivot]

return(c(quick_sort(less), equal, quick_sort(greater)))


}

# Binary Search function (assumes the array is sorted)


binary_search <- function(arr, target) {
left <- 1
right <- length(arr)

while (left <= right) {


mid <- floor((left + right) / 2)

if (arr[mid] == target) {
return(mid) # Found the target element
} else if (arr[mid] < target) {
left <- mid + 1
} else {
right <- mid - 1
}
}

return(-1) # Target element not found


}
BCA- V- R-LAB MANUAL Page 14
SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

# Example data
data <- c(5, 8, 1, 6, 3, 7, 2, 4)
target <- 6

# Sort the data using Quick Sort


sorted_data <- quick_sort(data)

# Perform Binary Search to find the target


result <- binary_search(sorted_data, target)

# Print the result


if (result != -1) {
cat("Element", target, "found at index", result, "\n")
} else {
cat("Element", target, "not found in the sorted array.\n")
}

OUTPUT

Element 6 found at index 6

BCA- V- R-LAB MANUAL Page 15


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
8. Write an R Program Illustrate Reading & Writing Files.

In this example, we'll read data from an existing text file, perform a simple
operation on the data, and then write the result to a new text file.

First create a text file input.txt in the same location as the prg8.R script and type
“Hello, this is a test.” And save it as shown below

Type the R script in R Studio


# Define the file paths
input_file <-"F:/R-Program/lab/input.txt"
output_file <-"F:/R-Program/lab/output.txt"

# Reading data from a file


cat("Reading data from input file...\n")
tryCatch({
data <- readLines(input_file)
cat("Data read successfully!\n")
}, error = function(e) {
cat("Error reading data from the input file:", e$message,
"\n")
data <- NULL
})

# Check if data was successfully read


if (!is.null(data)) {

BCA- V- R-LAB MANUAL Page 16


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
# Perform a simple operation (e.g., convert text to
uppercase)
modified_data <- toupper(data)

# Writing data to an output file


cat("Writing modified data to output file...\n")
tryCatch({
writeLines(modified_data, output_file)
cat("Data written successfully!\n")
}, error = function(e) {
cat("Error writing data to the output file:",
e$message, "\n")
})
}

Then Select all and click on RUN


OUTPUT
Reading data from input file...
Data read successfully!
Writing modified data to output file...
Data written successfully!

BCA- V- R-LAB MANUAL Page 17


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

The output.txt is created in same path with written as HELLO,


THIS IS A TEST as shown below.

Hello, this is a test.

BCA- V- R-LAB MANUAL Page 18


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
The output might look like this:

Reading data from input file...


Data read successfully!
Writing modified data to output file...
Data written successfully!

9. Write a R program for any visual representation of an object with creating


graphs using graphic functions:
Plot(),Hist(),Linechart(),Pie(),Boxplot(),Scatterplots().

# Create sample data


x <- c(1, 2, 3, 4, 5)
y <- c(3, 5, 7, 2, 8)

# Create a new graphics device (window)


dev.new()

# Create a plot
plot(x, y, type = "o", main = "Line Chart", xlab = "X-
axis", ylab = "Y-axis", col = "blue")

# Create a new graphics device


dev.new()

# Create a histogram
hist(x, main = "Histogram", xlab = "Value", ylab =
"Frequency", col = "lightgreen")

# Create a new graphics device


dev.new()

# Create a line chart


lines(x, y, type = "o", col = "red")
title(main = "Line Chart", xlab = "X-axis", ylab = "Y-
axis")

# Create a new graphics device

BCA- V- R-LAB MANUAL Page 19


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
dev.new()

# Create a pie chart


pie(x, labels = c("A", "B", "C", "D", "E"), main = "Pie
Chart")

# Create a new graphics device


dev.new()

# Create a boxplot
boxplot(x, main = "Boxplot", ylab = "Values", col =
"orange")

# Create a new graphics device


dev.new()

# Create a scatterplot
plot(x, y, main = "Scatterplot", xlab = "X-axis", ylab =
"Y-axis", col = "green", pch = 19)

best way to Run this program is select one by one


and Run it and observe graph as output
OR
Divide the program in to sub small programs for
easy understand with good output
Write an R program for any visual representation of an object with creating
graphs using graphic functions: Plot()

# Create sample data


x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 3, 5)

# Create a scatter plot


plot(x, y,

BCA- V- R-LAB MANUAL Page 20


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
main = "Simple Scatter Plot",
xlab = "X-axis",
ylab = "Y-axis",
col = "blue",
pch = 16
)

# Add a title and labels to the axes


title(main = "Simple Scatter Plot", col.main = "darkblue",
font.main = 4)

OUTPUT

BCA- V- R-LAB MANUAL Page 21


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

Write an R program for any visual representation of an object with creating


graphs using graphic functions: Hist()

# Create a vector of sample data


data <- c(22, 30, 31, 35, 36, 40, 42, 45, 50, 55, 60, 70,
75, 80, 85)

# Create a histogram
hist(data,
main = "Sample Histogram",
xlab = "Values",
ylab = "Frequency",
col = "skyblue",
border = "black",
breaks = 5
)

# Add a title and labels to the axes


title(main = "Sample Histogram", col.main = "blue",
font.main = 4)

BCA- V- R-LAB MANUAL Page 22


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

OUTPUT

Write an R program for any visual representation of an object with creating


graphs using graphic functions: Linechart()

R doesn't have a specific function called LineChart() for creating line charts. However, you can
create a simple line chart using the base R graphics system with the plot() and lines()
functions. Here's a basic example

# Create sample data

BCA- V- R-LAB MANUAL Page 23


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
x <- 1:5
y <- c(3, 5, 7, 6, 9)

# Create a line chart


plot(x, y, type = "l", col = "blue", xlab = "X-axis",
ylab = "Y-axis", main = "Sample Line Chart")

# Add points
points(x, y, col = "red", pch = 19)

# Add gridlines
grid()

OUTPUT

BCA- V- R-LAB MANUAL Page 24


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

Write an R program for any visual representation of an object with creating


graphs using graphic functions: Pie()

# Sample data for the pie chart


slices <- c(40, 25, 20, 15)
labels <- c("A", "B", "C", "D")

# Create a pie chart


pie(slices, labels = labels, col =
rainbow(length(slices)))

# Add a title
title(main = "Sample Pie Chart")

BCA- V- R-LAB MANUAL Page 25


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

OUTPUT

Write an R program for any visual representation of an object with creating


graphs using graphic functions: Boxplot()

# Sample data for the boxplot


data <- list(
Group1 = c(30, 35, 40, 45, 50, 55, 60, 65, 70),

BCA- V- R-LAB MANUAL Page 26


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
Group2 = c(25, 30, 35, 40, 45, 50, 55, 60, 65)
)

# Create a boxplot
boxplot(data,
col = c("blue", "green"),
main = "Sample Boxplot",
xlab = "Groups",
ylab = "Values")

# Add a legend
legend("topright", legend = c("Group1", "Group2"), fill =
c("blue", "green"))

OUTPUT

BCA- V- R-LAB MANUAL Page 27


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

Write an R program for any visual representation of an object with creating


graphs using graphic functions: Scatterplots()

You can use the plot() function to create a scatterplot with your data

# Sample data for the scatterplot


x <- c(3, 5, 7, 6, 9)
y <- c(2, 4, 6, 8, 10)

# Create a scatterplot
plot(x, y,
xlab = "X-axis",
ylab = "Y-axis",
main = "Sample Scatterplot",
col = "blue",
pch = 19
)

BCA- V- R-LAB MANUAL Page 28


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

OUTPUT

12. Write an R Program to Find Mean, Mode & Median.

Data set= 3, 3, 3, 4, 4, 4, 5, 5, 5
In this dataset:

Mean: Mean = (3 + 3 + 3 + 4 + 4 + 4 + 5 + 5 + 5) / 9 = 36 / 9 = 4

BCA- V- R-LAB MANUAL Page 29


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
Median: Since the dataset is already sorted, the median is the middle value, which
is 4.

Mode: The mode is the most frequently occurring value, and in this dataset, the
value 4 appears most frequently.

So, for the dataset 3, 3, 3, 4, 4, 4, 5, 5, 5, the mean, median, and mode


are all equal to 3 ,4 ,5

Program
# Create the dataset
data <- c(3, 3, 3, 4, 4, 4, 5, 5, 5)

# Calculate the mean


mean_value <- mean(data)

# Calculate the median


median_value <- median(data)

# Calculate the mode


find_mode <- function(x) {
uniq_x <- unique(x)
freq_x <- table(x)
max_freq <- max(freq_x)
mode <- as.numeric(names(freq_x[freq_x == max_freq]))
return(mode)
}

mode_value <- find_mode(data)

# Print the results


cat("Mean:", mean_value, "\n")
cat("Median:", median_value, "\n")
cat("Mode:", mode_value, "\n")

BCA- V- R-LAB MANUAL Page 30


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

OUTPUT
> cat("Mean:", mean_value, "\n")
Mean: 4
> cat("Median:", median_value, "\n")
Median: 4
> cat("Mode:", mode_value, "\n")
Mode: 3 4 5

10. Write a R program for with any dataset containing data frame objects,
and employ manipulating and analyzing data.
R program that demonstrates basic data manipulation and analysis using a dataset. In this
example, we'll use the built-in mtcars dataset, which contains information about various car
models:

# Load the mtcars dataset (if not already loaded)


data(mtcars)

# Display the first few rows of the dataset


head(mtcars)

# Calculate the mean and median of miles per gallon


(mpg)
mean_mpg <- mean(mtcars$mpg)
median_mpg <- median(mtcars$mpg)

BCA- V- R-LAB MANUAL Page 31


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

# Display the mean and median


cat("Mean MPG:", mean_mpg, "\n")
cat("Median MPG:", median_mpg, "\n")

# Find the car with the highest miles per gallon (mpg)
max_mpg_car <- mtcars[mtcars$mpg == max(mtcars$mpg), ]

# Display the car with the highest MPG


cat("Car with the highest MPG:\n")
print(max_mpg_car)

# Filter cars with more than 100 horsepower


high_hp_cars <- mtcars[mtcars$hp > 100, ]

# Display cars with more than 100 horsepower


cat("Cars with more than 100 HP:\n")
print(high_hp_cars)

OUTPUT
# Display the first few rows of the dataset
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
>
> # Calculate the mean and median of miles per gallon (mpg)
> mean_mpg <- mean(mtcars$mpg)
> median_mpg <- median(mtcars$mpg)
>
> # Display the mean and median
> cat("Mean MPG:", mean_mpg, "\n")
Mean MPG: 20.09062
> cat("Median MPG:", median_mpg, "\n")
Median MPG: 19.2
>
> # Find the car with the highest miles per gallon (mpg)
> max_mpg_car <- mtcars[mtcars$mpg == max(mtcars$mpg), ]
>
> # Display the car with the highest MPG
> cat("Car with the highest MPG:\n")
Car with the highest MPG:
> print(max_mpg_car)

BCA- V- R-LAB MANUAL Page 32


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
mpg cyl disp hp drat wt qsec vs am gear carb
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.9 1 1 4 1
>
> # Filter cars with more than 100 horsepower
> high_hp_cars <- mtcars[mtcars$hp > 100, ]
>
> # Display cars with more than 100 horsepower
> cat("Cars with more than 100 HP:\n")
Cars with more than 100 HP:
> print(high_hp_cars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

BCA- V- R-LAB MANUAL Page 33


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

OR IN SIMPLE WAY

Simple dataset and perform some basic data manipulation and analysis. We'll create a custom
dataset for this example and demonstrate operations such as summarizing data and subsetting.
Here's the R program:

# Create a simple custom dataset (data frame)


data <- data.frame(
StudentID = 1:10,
Age = c(22, 21, 23, 20, 22, 24, 19, 20, 21, 22),
Score = c(85, 92, 78, 65, 89, 73, 97, 81, 88, 90)
)

# Display the dataset


print(data)

# Calculate the mean and median of the 'Score' column


mean_score <- mean(data$Score)
median_score <- median(data$Score)

cat("Mean Score:", mean_score, "\n")


cat("Median Score:", median_score, "\n")

# Filter students older than 21


older_students <- data[data$Age > 21, ]

cat("Students older than 21:\n")


print(older_students)

OUTPUT
# Create a simple custom dataset (data frame)
> data <- data.frame(
+ StudentID = 1:10,
+ Age = c(22, 21, 23, 20, 22, 24, 19, 20, 21, 22),
+ Score = c(85, 92, 78, 65, 89, 73, 97, 81, 88, 90)
+ )
>
> # Display the dataset
> print(data)
StudentID Age Score
1 1 22 85

BCA- V- R-LAB MANUAL Page 34


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
2 2 21 92
3 3 23 78
4 4 20 65
5 5 22 89
6 6 24 73
7 7 19 97
8 8 20 81
9 9 21 88
10 10 22 90
>
> # Calculate the mean and median of the 'Score' column
> mean_score <- mean(data$Score)
> median_score <- median(data$Score)
>
> cat("Mean Score:", mean_score, "\n")
Mean Score: 83.8
> cat("Median Score:", median_score, "\n")
Median Score: 86.5
>
> # Filter students older than 21
> older_students <- data[data$Age > 21, ]
>
> cat("Students older than 21:\n")
Students older than 21:
> print(older_students)
StudentID Age Score
1 1 22 85
3 3 23 78
5 5 22 89
6 6 24 73
10 10 22 90

>

BCA- V- R-LAB MANUAL Page 35


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

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


multivariate context for predictive purpose.
Linear regression in a multivariate context involves predicting a target variable (dependent
variable) based on multiple predictor variables (independent variables). Here's a simple R
program that demonstrates multivariate linear regression for predictive purposes using a sample
dataset:

# Create a custom dataset


data <- data.frame(
Age = c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70),
Income = c(35000, 40000, 45000, 50000, 55000, 60000,
65000, 70000, 75000, 80000),
Education = c(12, 14, 16, 16, 18, 20, 20, 22, 24, 24)
)

# Display the dataset


print(data)

# Create a linear regression model


lm_model <- lm(Income ~ Age + Education, data = data)

# Summary of the linear regression model


summary(lm_model)

BCA- V- R-LAB MANUAL Page 36


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES

# Make a prediction using the model


new_data <- data.frame(Age = 35, Education = 16)
predicted_income <- predict(lm_model, newdata =
new_data)

# Display the prediction


cat("Predicted Income:", predicted_income, "\n")

OUTPUT
# Create a custom dataset
> data <- data.frame(
+ Age = c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70),
+ Income = c(35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000),
+ Education = c(12, 14, 16, 16, 18, 20, 20, 22, 24, 24)
+ )
>
> # Display the dataset
> print(data)
Age Income Education
1 25 35000 12
2 30 40000 14
3 35 45000 16
4 40 50000 16
5 45 55000 18
6 50 60000 20
7 55 65000 20
8 60 70000 22
9 65 75000 24
10 70 80000 24
>
> # Create a linear regression model
> lm_model <- lm(Income ~ Age + Education, data = data)
>
> # Summary of the linear regression model
> summary(lm_model)
Call:
lm(formula = Income ~ Age + Education, data = data)
Residuals:
Min 1Q Median 3Q Max
-2.462e-12 -1.433e-12 -1.020e-12 1.287e-12 4.776e-12

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.000e+04 9.585e-12 1.043e+15 <2e-16 ***
Age 1.000e+03 4.255e-13 2.350e+15 <2e-16 ***
Education -1.124e-12 1.565e-12 -7.180e-01 0.496
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.736e-12 on 7 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 1.378e+32 on 2 and 7 DF, p-value: < 2.2e-16

Warning message:

BCA- V- R-LAB MANUAL Page 37


SMT. KUMUDBEN DARBAR COLLEGE OF COMMERCE,SCIENCE &
MANAGEMENT STUDIES
In summary.lm(lm_model) :
essentially perfect fit: summary may be unreliable
>
> # Make a prediction using the model
> new_data <- data.frame(Age = 35, Education = 16)
> predicted_income <- predict(lm_model, newdata = new_data)
>
> # Display the prediction
> cat("Predicted Income:", predicted_income, "\n")
Predicted Income: 45000

>

END

BCA- V- R-LAB MANUAL Page 38

You might also like