R Programming Lab 14102024
R Programming Lab 14102024
LABORATORY MANUAL
B.Tech. Semester VIII
CSE/CSIT/IT
Name:
Roll. No.:
Group/Branch
:
Vision:
“Empowering human values and advanced technical education to navigate and address global
Mission:
M2: Supporting the cultivation of a new generation of innovators who are not only
M3: Inspire global citizens who are equipped to create positive and sustainable impact, driving
progress towards a more inclusive and harmonious worl
Vision:
Mission:
PEO1: Apply the technical competence in Computer Science and Engineering for solving
PEO2: Carry out research and develop solutions on problems of social applications.
PEO3: Work in a corporate environment, demonstrating team skills, work morals, flexibility
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
PO9: Individual and teamwork: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10: Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
P11: Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team,
to manage projects and in multidisciplinary environments.
P12: Life-long learning: Recognize the need for and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PSO1: Exhibit design and programming skills to develop and mechanize business solutions
PSO2: Learn strong theoretical foundation leading to brilliance and enthusiasm towards
PSO3: Work effectively with diverse Engineering fields as a team to design, build and develop
system applications.
Course code
Category Laboratory course
Course title R - Programming Lab
Scheme and Credits L T P Credits
Semester =
0 0 2 1
Classwork 50 Marks
Exam 50 Marks
Total 100 Marks
Duration of Exam 02 Hours
1. Download and install R-Programming environment and install basic packages using
install.packages() command in R.
2. Learn all the basics of R-Programming (Data types, Variables, Operators etc.
3. Implement R-Loops with different examples
4. Learn the basics of functions in R and implement with examples.
5. Implement data frames in R. Write a program to join columns and rows in a data frame using cbind()
and rbind() in R
6. Implement different String Manipulation functions in R.
7. Implement different data structures in R (Vectors, Lists, Data Frames).
8. Write a program to read a csv file and analyze the data in the file in R.
9. Create pie charts and bar charts using R.
10. Create a data set and do statistical analysis on the data using R
CO-PO Mapping
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 3 2 2 - - - - - - - - -
CO2 2 2 2 - - - - - - - - -
CO-PSO Mapping
CO PSO1 PSO2 PSO3 PSO4
CO1 3 2 2 -
CO2 2 3 2 -
CO3 2 3 2 -
CO4 3 2 2 -
CO5 3 2 1 -
C06 3 2 2 -
*3-HIGH
*2-MEDIUM
*1-LOW
Course Overview
The R-Programming Lab is a practical hands-on course designed to provide students with practical
experience in statistics and data analysis applications. It aims to complement the theoretical concepts taught
in lectures by giving students the opportunity to apply their knowledge in a real-world data analysis
environment.
8. Write a program to read a csv file and analyze the data in the file in R
10. Create a data set and do statistical analysis on the data using R
DON’Ts
1. Turn the equipment off. If power switch is not immediately accessible, take plug off.
2. If fire continues, try to curb the fire, if possible, by using the fire extinguisher or by covering
it with a heavy cloth if possible, isolate the burning equipment from the other surrounding
equipment.
3. Sound the fire alarm by activating the nearest alarm switch located in the hallway.
4. Call security and emergency department immediately:
1) All files must contain a title page followed by an index page. The files will not be signed by
the faculty without an entry in the index page.
2) Student’s Name, roll number and date of conduction of experiment must be written on all
pages.
3) For each experiment, the record must contain the following
(i) Aim/Objective of the experiment
(ii) Pre-experiment work (as given by the faculty)
(iii) Lab assignment questions and their solutions
(iv) Test Cases (if applicable to the course)
(v) Results/ output
Note:
1. Students must bring their lab record along with them whenever they come for the lab.
2. Students must ensure that their lab record is regularly evaluated.
AC4: All variants of input All variants of input Only few variants Solution is not well
Execution & /output are tested, /output are not of input /output are demonstrated and
Demonstration Solution is well tested, However, tested, implemented
demonstrated and solution is well Solution is well concept is not
implemented demonstrated and demonstrated but clearly explained
concept is clearly implemented implemented
explained concept is clearly concept is not
explained clearly explained
Q 2: What is R, and what is it commonly used for in the context of data analysis and statistics?
OBJECTIVE:
Download and install R-Programming environment and install basic packages using install.packages()
command in R.
install.packages("dplyr")
install.packages("ggplot2")
install.packages("tidyr")
library(dplyr)
library(ggplot2)
library(tidyr)
OUTPUT
Now, you have R installed, and you've installed and loaded some basic packages. You can start using these
packages for data manipulation, visualization, and analysis.
OBJECTIVE
Learn all the basics of R-Programming (Data types, Variables, Operators etc.)
1. Data Types:
Numeric:
num_var <- 42
Character:
char_var <- "Hello, R!"
Logical:
logical_var <- TRUE
Factor:
factor_var <- factor(c("High", "Medium", "Low"))
2. Variables:
age <- 25
3. Operators:
Arithmetic Operators:
sum_result <- 5 + 3
prod_result <- 4 * 6
div_result <- 8 / 2
Comparison Operators:
is_equal <- (5 == 5)
not_equal <- (3 != 7)
Logical Operators:
logical_and <- TRUE & FALSE
4. Vectors:
numeric_vector <- c(1, 2, 3, 4, 5)
5. Matrices:
matrix_data <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
6. Lists:
my_list <- list(name = "John", age = 30, is_student = FALSE)
7. Data Frames:
data_frame <- data.frame(name = c("Alice", "Bob", "Charlie"), age = c(25, 30, 22))
OUTPUT
Data Type
Variable
[1] 25
[1] "Alice"
[1] TRUE
Operators
[1] 8
[1] 5
[1] 24
[1] 4
Vectors
[1] 1 2 3 4 5
[1] "apple" "banana" "orange"
[1] TRUE FALSE TRUE
Matrices
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Lists
$name
[1] "John"
$age
[1] 30
Q2. Were you able to successfully apply the basics of R programming to accomplish specific tasks or solve
problems?
Q3. What challenges did you encounter during the learning process, and how did you overcome them?
Q 2: What is your current understanding or familiarity with programming loops, especially in the context of
R?
OBJECTIVE
Implement R-Loops with different examples.
for (i in 1:5) {
print(paste("Iteration:", i))
This loop iterates over the values from 1 to 5, and in each iteration, it prints a message.
j <- 1
while (j <= 5) {
print(paste("Iteration:", j))
j <- j + 1
This loop does the same thing as the for loop, but it uses a while loop instead.
k <- 1
repeat {
print(paste("Iteration:", k))
k <- k + 1
if (k > 5) {
his example uses a repeat loop, which continues indefinitely until the break statement is encountered.
OUTPUT
Q 2: Were you able to successfully implement loops in R to accomplish specific tasks or solve problems?
Q 3: What challenges did you encounter during the implementation, and how did you overcome them?
LAB EXPERIMENT 4
OBJECTIVE
Learn the basics of functions in R and implement with examples.
Defining a Function:
You can define a function in R using the function keyword. The basic syntax is as follows:
# Function body
print("Hello, World!")
# Function call
hello_world()
result <- a + b
return(result)
# Function call
print(sum_result)
OUTPUT
[1] 10
Q 1: How well did you grasp the basics of functions in R during the learning process?
Q 2: Were you able to successfully implement functions in R to accomplish specific tasks or solve problems?
LAB EXPERIMENT 5
Q2. Why have you chosen the cbind() and rbind() operations for your experiment? How do
these operations align with your research objectives?
OBJECTIVE
Implement data frames in R. Write a program to join columns and rows in a data frame
using cbind() and rbind() in R
OUTPUT
Original Data Frame 1:
ID Name Age
1 1 Alice 25
2 2 Bob 30
3 3 Charlie 22
2 2 Bob 30
3 3 Charlie 22
4 4 David 28
5 5 Eve 35
6 6 Frank 29
Q 2: To what extent did cbind() and rbind() meet the intended functionality? Were there any
limitations or unexpected benefits in terms of flexibility?
LAB EXPERIMENT 6
Q 2: Why have you chosen particular string manipulation functions for your experiment? How
do these functions align with your research objectives??
OBJECTIVE
Implement different String Manipulation functions in R.
1. Concatenation:
# Concatenate strings
2. Substring Extraction:
# Extract substring
3. String Length:
# Calculate string length
5. String Replacement:
# Replace a substring
6. Splitting Strings:
# Split a string
OUTPUT
Concatenated String: Hello World
Substring: Scien
String Length: 11
Q 2: How readable and usable was the code using these string manipulation functions? Did any
of them enhance or hinder code clarity?
LAB EXPERIMENT 7
Q2. Why have you chosen vectors, lists, and data frames for your experiment? How do these
data structures align with your research objectives??
Q3. What challenges or difficulties do you anticipate in implementing and working with these
data structures?
OBJECTIVE
Implement different data structures in R (Vectors, Lists, Data Frames).
1. Vectors:
Vectors are one-dimensional arrays that can hold elements of the same data type.
print(numeric_vector[3]) # Output: 3
2. Lists:
Lists are collections of elements that can be of different data types. Each element in a list can
be a vector, list, or any other R object.
# Creating a list
3. Data Frames:
Data frames are two-dimensional structures, similar to a table or spreadsheet, with rows and
columns.
print(my_data[2, 3]) # Output: Element in the second row and third column
Output:
[1] 3
[1] 1 2 3 4 5
[1] B
Levels: A B C
Q2. How readable and maintainable was the code using these data structures? Did any of them
enhance or hinder code clarity?
LAB EXPERIMENT 8
Q2. Where does the CSV file come from, and what kind of data does it contain??
Q3. What steps will you take for data cleaning and preprocessing before performing the
analysis??
OBJECTIVE
Write a program to read a csv file and analyze the data in the file in R.
Assuming you have a CSV file named "data.csv" with the following content:
Alice, 25, A
Bob, 30, B
Charlie, 22, C
Now, let's write a program to read and analyze this CSV file:
print(data)
# Summary statistics
# Mean of Age
# Maximum Age
# Minimum Age
OUTPUT
1 Alice 25 A
2 Bob 30 B
3 Charlie 22 C
This output includes the displayed data, summary statistics for the "Age" column, mean age,
maximum age, and minimum age based on the content of the provided CSV file.
Q2. How would you describe the quality of the data in the CSV file? Were there any issues
encountered during data analysis that might indicate data quality concerns??
LAB EXPERIMENT 9
Q2. What are the key variables you plan to represent using pie charts and bar charts?
Q3. Why have you chosen pie charts and bar charts specifically for your visualizations? What
insights do you expect to gain from each??
OBJECTIVE
Create pie charts and bar charts using R.
1. Pie Chart:
# Data for the pie chart
labels <- c("Category A", "Category B", "Category C", "Category D")
2. Bar Chart:
# Data for the bar chart
categories <- c("Category A", "Category B", "Category C", "Category D")
OUTPUT
Q1. How effective were the pie charts and bar charts in communicating the information you
intended?
Q2. What insights did you gain from the visualizations that were not immediately apparent in
the raw data.?
Q3. Were the visualizations accurate and precise in representing the underlying data?
LAB EXPERIMENT 10
OBJECTIVE
Create a data set and do statistical analysis on the data using R.
# Create a dataset
print(head(my_data))
# Descriptive statistics
print("Descriptive statistics:")
print(summary(my_data))
print(t_test_result)
OUTPUT
Height Weight
1 173.4392 66.00146
2 160.7693 70.73500
3 174.3770 74.89562
4 163.6827 73.63305
5 175.4874 71.47079
6 167.8282 70.05956
Height Weight
-3.658126 1.451653
sample estimates:
mean of x mean of y
169.8056 170.7962
Q2 What does the t-test result suggest about the difference in means between the two groups?