R PROGRAMMING LAB MANUAL
R PROGRAMMING LAB MANUAL
Regulation : R22
LIST OF EXPERIMENTS:
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. Write R command to
i) Illustrate summation, subtraction, multiplication, and division operations
on vectors using vectors.
ii)Enumerate multiplication and division operations between matrices and
vectors in R console
4. Write R command to
8. Writean R program to read a csv file and analyze the data in the file using EDA
(Explorative Data Analysis) techniques.
9. Writean R program to illustrate Linear Regression and Multi linear Regression
considering suitable CSV file
Experiment 1:Download and install R-Programming environment and install basic
packages using install. packages() command in R.
2) Click on the R 3.2.2.exe file. The 3.2.2 is the version number of the file. The versions can be
updated as per the latest releases.
2) The SetUp will request permission to be installed on the system click yes to p
1
4) Select the preferred language from the drop down to begin an installation in that preferred
language.
2
6.Choose the path where you wish to install R by clicking on browse and changing the
workspace locations. Click next to proceed with the default installation. Theminimum space
requirementsare mentioned at the bottomof the dialog box. Please check you have required
amount of free space in your drive
7) Choose the type of installation you require. By default R installs both the 32 and 64 bit
versions on your system.
8) To customize the startup options for R choose option and customize. To proceed with a
vanilla installation use Next.
3
9) Generate program short cuts and naming those as per your requirement specify the
necessary customizations. To proceed with the default installation hit next.
4
10) Click on the next button to begin installation.
11) After the installation has completed you will see the final screen.Click finish to
complete the installation.
5
12) Open Start Menu and you will find R in the available set of Programs.
6
Experiment 2:Learn all the basics of R-Programming (Data types, Variables, Operators etc,.)
The variables are assigned with R-Objects and the data type of the R-object becomes the data
type of the variable. There are many types of R-objects. The frequently used are:
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
The simplest of these objects is the vector object and there are six data types of these atomic
vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic
vectors.
In R, c() function is used to create a vector. This function returns a one-dimensional array or
simply vector. The c() function is a generic function which combines its argument. All
arguments are restricted with a common data type which is the type of the returned value. There
are various other ways to create a vector in R, which are as follows:
We can create a vector with the help of the colon operator. There is the following syntax to use
colon operator:
z<-x:y
Vectors :Tocreate vector with more than one element, use c() function which means combine
the elements into a vector.
# Create a vector.
print(apple)
print(class(apple))
7
[1] "red" "green" "yellow"
[1] "character"
Lists
A list is an R-object which can contain many different types of elements inside it like vectors,
functions and even another list inside it.
# Create a list.
print(list1)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
Matrices
A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the
matrix function.
# Create a matrix.
print(M)
8
[2,] "c" "b" "a"
Arrays
While matrices are confined to two dimensions, arrays can be of any number of dimensions. The
array function takes a dim attribute which creates the required number of dimension. In the
below example we create an array with two elements which are 3x3 matrices each.
# Create an array.
a <- array(c('green','yellow'),dim=c(3,3,2))
print(a)
,,2
Variable: A variable provides us with named storage that our programs can manipulate. A
variable in R can store an atomic vector, group of atomic vectors or a combination of many R-
objects. A valid variable name consists of letters, numbers and the dot or underline characters.
The variable name starts with a letter or the dot not followed by a number.
9
2var_name invalid Starts with a number
Operators:An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. R language is rich in built-in operators and provides following types of
operators.
Types of Operators :
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
!
Arithmetic Operators
Operator Description
> Checks if each element of the first vector is greater than the corresponding
element of the second vector.
< Checks if each element of the first vector is less than the corresponding
element of the second vector
<= Checks if each element of the first vector is less than or equal to the
corresponding element of the second vector.
10
>= Checks if each element of the first vector is greater than or equal to the
corresponding element of the second vector
Logical Operators
Below table shows the logical operators supported by R language. It is applicable only to vectors
of type logical, numeric or complex. All numbers greater than 1 are considered as logical value
TRUE.
Each element of the first vector is compared with the corresponding element of the second
vector. The result of comparison is a Boolean value.
Operator Description
& It is called Element-wise Logical AND operator. It combines each element of the
first vector with the corresponding element of the second vector and gives a
output TRUE if both the elements are TRUE
! It is called Logical NOT operator. Takes each element of the vector and gives the
opposite logical value
Assignment Operators :
These operators are used to assign values to vectors.
Operator Description
<-
or
= Called Left Assignment
or
<<-
-> Called Right Assignment
or
->>
11
Experiment 3:Write R command to
a) Illustrate summation, subtraction, multiplication, and division operations on
vectors using vectors.
PROGRAM:
Summation of vectors:
# Perform summation
print(sum_result)
Subtraction of of vectors:
# Perform subtraction
print(sub_result)
12
Multiplication of vectors:
# Perform multiplication
print(mul_result)
Division of vectors:
# Perform division
print(div_result)
13
Experiment 4 :Write R command to
i) Illustrates the usage of Vector subsetting and Matrix subsetting
ii) Write a program to create an array of 3×3 matrixes with 3 rows and 3
columns.
subset_
vector <- my _vector[c(2, 4)] # Selecting elements at index 2 and 4
print(subset _vector)
14
ii) Write a program to create an array of 3×3 matrices with 3 rows and 3 columns:
PROGRAM:
15
Experiment 5 - Write an R program to draw i) Pie chart ii) 3D Pie Chart, iii) Bar Chart along
with chart legend by considering suitable CSV file
PROGRAM:
i. Pie chart
# color pallet.
col = rainbow(length(geeks)))
16
3D pie chart: A 3D pie chart showing the same data as the regular pie chart
PROGRAM:
17
Bar chart: A bar chart showing the values of the variable_for_chart variable for each category in
the category _variable variable.
PROGRAM:
colors = c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")
# Create the matrix of the values.
Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
nrow = 3, ncol = 5, byrow = TRUE)
# Create the bar chart
barplot(Values, main = "Total Revenue", names.arg = months,
xlab = "Month", ylab = "Revenue", col = colors)
# Add the legend to the chart
legend("topleft", regions, cex = 0.7, fill = colors)
18
Experiment 6 Create a CSV file having Speed and Distance attributes with 1000 records. Write
R program to draw
a) Box plots
b) Histogram
c) Line Graph
d) Multiple line graphs
e) Scatter plot
a) Box plots
b) Histogram
PROGRAM:
# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32,
14, 19, 27, 39)
# Create the histogram.
hist(v, xlab = "No.of Articles ",
col = "green", border = "black")
19
c) Line Graph
PROGRAM:
PROGRAM:
library("ggplot2")
20
e) Scatter plot
PROGRAM:
21
Experiment 7-Implement different data structures in R (Vectors, Lists, Data Frames)
Vectors
PROGRAM:
25
X = c(1, 3, 5, 7, 8)
# Printing those elements in console
print(X)
Lists:
Lists:
Lists can hold elements of different data types and can be nested.
empId = c(1, 2, 3, 4)
numberOfEmp = 4
print(empList)
22
PROGRAM:
26
print(df)
23
Experiment 8-Write an R program to read a csv file and analyze the data in the file using EDA
(Explorative Data Analysis) techniques.
library(readr)
library(dplyr)
library(ggplot2)
# Add more EDA visualizations and analyses as needed based on your data
dev. off()
24
PROGRAM :DATA INSPECTION IN EDA
# REGEX rules
p < - c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C","Cr", "R")
sort(unique(h$hzname))
h$hzname < - ifelse(h$hzname == "BT","Bt nu", h$hzname)
25
PROGRAM(GRAPHICAL METHOD)
# EDA Graphical Method Distributions
# REGEX rules
p <- c("A", "BA|AB", "Bt|Bw", "Bt3|Bt4|2B|C","Cr", "R")
26
sort(unique(h$hzname))
h$hzname <- ifelse(h$hzname == "BT", "Bt", h$hzname)
# graphs
# bar plot
ggplot(h, aes(x = texcl)) +geom_bar()
# histogram
ggplot(h, aes(x = clay)) +
geom_histogram(bins = nclass.Sturges(h$clay))
# density curve
ggplot(h, aes(x = clay)) + geom_density()
# box plot
ggplot(h, (aes(x = genhz, y = clay))) +
geom_boxplot()
27
Experiment 9-Write an R program to illustrate Linear Regression and Multi linear Regression
considering suitable CSV file.
# Linear Regression and Multi linear Regression example using the mtcars dataset
# Step 2: Load the mtcars dataset (or use your own CSV file)
data (mtcars)
28
PROGRAM(LINEAR REGRESSION)
# R program to illustrate
# Linear Regression
# Height vector
x <- c(153, 169, 140, 186, 128,136, 178, 163, 152, 133)
# Weight vector
y <- c(64, 81, 58, 91, 47, 57,75, 72, 62, 49)
# Plot
plot(x, y, main = "Height vs Weight Regression model")
abline(lm(y~x))
29
PROGRAM: MULTIPLE REGRESSION
# R program to illustrate
# Multiple Linear Regression
# Plot
plot(model)
30
Lead Programs:
1. Linear Discriminant Analysis is computed using the lda() function. Let’s use the iris data
set of R Studio.
library(MASS)
library(tidyverse)
library(caret)
theme_set(theme_classic())
# Split the data into training (80%) and test set (20%)
set.seed(123)
training.individuals <- iris$Species %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- iris[training.individuals, ]
test.data <- iris[-training.individuals, ]
# Make predictions
predictions <- model %>% predict(test.transform)
# Model accuracy
mean(predictions$class==test.transform$Species)
31
2. Decision Tree for Regression in R Programming.
32