19EAC385 R Programming Lab
Date: ..../....../......
Lab Sheet 2
Introduction to R programming
Aim
• To understand file handling, data manipulation, random number generation,
and visualization in R, focusing on key functions for reading, writing, and
analyzing datasets.
Introduction
This labsheet covers essential R functions for reading images and datasets, handling
data frames, generating random numbers, and visualizing data. It also explores
arithmetic operations on complex numbers, text manipulation, and statistical anal-
ysis using built-in datasets like mtcars. Through practical exercises, we will enhance
our proficiency in R for data science applications.
Examples
File reading
Figure 1: Image reading
Department of ECE 12 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
Data Frames
Figure 2: Data Frames using in-built dataset (mtcars)
Question 1 : Explain the functions with a suitable program
Figure 3
• read.table() – Reads a text file into a data frame with customizable delimiters.
• write.table() – Writes a data frame to a text file with specified formatting.
• write.csv() – Saves a data frame as a CSV file using commas as separators.
• readLines() – Reads a text file line by line into a character vector.
• writeLines() – Writes a character vector to a file, preserving line breaks.
Department of ECE 13 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
• fread() – Efficiently reads large CSV/text files using the data.table package.
Question 2: What is the difference between sample() and
set.seed()
Figure 4
• sample() is responsible for random selection
• set.seed() ensures the same randomness pattern is repeated for consistency in
results
Question 3:
A)write a function to roll a dice and print the results.
Figure 5
B) Write a program to tose a coin ten times and print the
result
Figure 6
Department of ECE 14 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
Graph Plotting
Figure 7
Question 4: Exercises
1. Take two vectors of complex numbers and perform arith-
metic operations on them:
Figure 8
Department of ECE 15 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
2. Use the mtcars dataset which is already available in R and
display the value of mpg column of those data rows where the
value of gear is 5.
Figure 9
3. Display a pie chart for cyl and hp separately.
Figure 10
Department of ECE 16 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
4. Write a program to convert a hexadecimal number into
its decimal equivalent.
Figure 11
5. Write a program that displays Oxford University Press as:
a) Oxford university press
b) OXFORD UNIVERSITY PRESS
c) .
d) OXFORDuNIVERSITYpRESS
Figure 12
6.Create a numeric vector by sampling 100 numbers from a
normal distribution with mean 2 and standard deviation 4.
a) Generate random numbers and then find out the number
of negative values
b) Calculate the standard deviation, mean and median of
your random numbers.
Department of ECE 17 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
c) Replace the last position in the vector with the letter X
and calculate the same summary statistics.
Figure 13
0.0.1 Inference: Replacing a numeric value with a character forces type
conversion, which affects computations
7. Write a program to demonstrate functions like sum(),
range(), min(), max(), order(), rep(), seq(), trunc(), apply(),
sapply(), lapply(), rapply().
Figure 14
Department of ECE 18 Sarang KP [AM.EN.U4EAC22061]
19EAC385 R Programming Lab
0.0.2 Inference: R provides powerful functions to handle data oper-
ations efficiently, including vectorized computations and apply-
family functions.
8. Use the mtcars dataset. Build a correlation plot. Plot
the different types of plot. You may need to install package
install packages(“corrplot”).
Figure 15
0.0.3 Inference: A correlation plot visually represents relationships be-
tween numerical variables, helping identify patterns.
Evaluation
Participation Knowledge Results Conduct Report Ethics Total
Name of the faculty:
Signature with date:
Department of ECE 19 Sarang KP [AM.EN.U4EAC22061]