Professional Documents
Culture Documents
Institute’s Mission
Department’s Mission
Certificate
has performed and successfully completed all the practical’s in the subject
of R Programming Lab
for the academic year 2020 to 2021 as prescribed by University of Mumbai.
DATE: - 19/05/2021.
__ __ __ _ __ __ __
COLLEGE SEAL
Program Outcomes
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
11. 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.
12. 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.
Department of Information Technology
Semester: VIII
Class: BE
ITL804.3 Extract data from files and other sources and perform
various data manipulation tasks on them.
ITL804.4 Code statistical functions in R
8
9
10
Total Grade / Marks:
(A+B)
Obtained Out of Obtained Out of
Practical Incharge
EXPERIMENT NO. 1
THEORY:
What is R?
R is a language and environment for statistical computing and graphics. It is a GNU project which
is similar to the S language and environment which was developed at Bell Laboratories (formerly
AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a
different implementation of S. There are some important differences, but much code written for S
runs unaltered under R.
The R environment
R is an integrated suite of software facilities for data manipulation, calculation and graphical
display. It includes
• an effective data handling and storage facility,
• a suite of operators for calculations on arrays, in particular matrices,
• a large, coherent, integrated collection of intermediate tools for data analysis,
• graphical facilities for data analysis and display either on-screen or on hardcopy, and
• a well-developed, simple and effective programming language which includes
conditionals, loops, user-defined recursive functions and input and output facilities.
Installation of R and R Studio:
1. Install R (https://www.r-project.org/)
2. Install R Studio (https://rstudio.com/products/rstudio/download/)
3. Initial Startup
4. Exploration of R Tools:
5. Installing Packages
CONCLUSION: Thus, we have successfully installed R and R Studio.
LAB OUTCOME:
ITL804.1: - Install and use R for simple programming tasks
ITL804.2: - Extend the functionality of R by using add-on packages
ITL804.3: - Extract data from files and other sources and perform various data manipulation tasks on them.
EXPERIMENT NO. - 2
Operators
Built-in Functions
Data Structures in R
Data Types - Vectors
R Data Structures - Matrices
Practical Incharge
Experiment No. 2
AIM: Perform Basic Operation On R.
✔ Operators
✔ Built-in Functions
✔ Data Structures In
R
✔ Data Types -
Vectors
✔ RData
Structures - Matrices
THEORY:
What Are Operators In R?
Function Meaning
log (x): log to base e of x
exp (x): antilog of x (ex)
log(x,n): log to base n of x
log10(x): log to base 10 of x
sqrt(x): square root of x
factorial(x): x!
floor(x): greatest integer <x
ceiling(x): smallest integer > x
trunc(x): closest integer to x between x and 0
cos(x),sine(x),tan(x): cosine of x (in radians), sine of x (in radians), tangent of x (in radians)
abs(x): the absolute value of x, ignoring the minus sign if there is one
R Data Structures
• Data Type – Vectors: Vectors are variables with one or more than one values of the
Same type: logical, integer, real, complex, string. Vectors could also have length 0
• a<-1.3
• a <- 5:10
• a<- c(5 6 7 8 9 10) using the concatenation function c,
• Types Of Vectors:
Vector Functions:
Operation Meaning
order(x): an integer vector containing the permutation to sort x into ascending order
Example:
Output:
100 4 25 6
50 2 50 2
75 3 75 3
150 5 100 1
200 6 150 4
25 1 200 5
Matrices:
All columns in a matrix must have the Same mode(numeric, character, etc.) and the Same
length.
• char_vector_rownames, char_vector_colnames))
[1,] 1 3
2 4
[2,]
colnames(X)<-c(1:4,"variance") rownames(X)<-c(1:3,"mean") X
1 2 3 4 variance
1 50 90 50 80 425.0000
2 60 100 90 30 1000.0000
3 40 80 10 70 1000.0000
mean 50 90 50 60 358.3333
Operators:
Arithmetic Operator:
Relational Operator:
Logical Operator:
Assignment Operator:
Miscellaneous Operator:
Built-In Functions in R:
R Data Structure (Data Types – Vector):
R Data Structure - Matrices:
CONCLUSION: Hence, we learned about all the types of basic operations in R Programming
along with their use, implementation, and examples.
LAB OUTCOME:
Lab Outcome: ITL804.1: - Install and use R for simple programming tasks
ITL804.4: - Code statistical functions in R
Practical Incharge
EXPERIMENT NO. 3
AIM: Grouping Loops and Conditional Execution
THEORY:
Control Flow and loops
1) IF ELSE:
Syntax: ifelse(test_expression, x, y)
Examples:
1) a = c(5,7,2,9)
ifelse(a %% 2 == 0,"even","odd")
2) x <- 5
if(x > 0)
{
print("Positive number")
}
3) x <-5
if(x > 0)
{
print("Non-negative number")
}
else
{
print("Negative number")
2) For loop
x <- c(2,5,3,9,8,11,6)
count <- 0
for (val in x)
{
if(val %% 2 == 0)
count = count+1
}
print(count)
3) While loop
i <- 1
while (i < 6)
{
print(i)
i = i+1
}
4) Break statement
x <- 1:5
for (val in x)
{
if (val == 3)
{
Break
}
print(val)
}
5) Next statement
x <- 1:5
for (val in x)
{
if (val == 3)
{
next
}
print(val)
}
Functions:
pow <- function(x, y)
{
result <- x^y
print(paste(x,"raised to the power", y, "is“, result))
}
>pow(8, 2)
"8 raised to the power 2 is 64"
>pow(2, 8)
"2 raised to the power 8 is 256"
OUTPUT:
1) IF-ELSE
a = c (5,7,2,9)
ifelse (a %% 2 == 0,"Even","Odd")
IF-ELSE
x = -5
if (x > 0)
{
print ("Non-negative number")
}
else
{
Print ("Negative number")
}
2) FOR LOOP
x = c (2,5,3,9,8,11,6)
count = 0
for (val in x)
{
if (val %% 2 == 0)
count = count+1
}
print(count)
3) WHILE LOOP
i=1
while (i < 6)
{
print(i)
i = i+1
}
6) FUNCTIONS:
pow = function(x, y)
{
result = x^y
print(paste(x,"raised to the power", y, "is", result))
}
pow(8, 2)
"8 raised to the power 2 is 64"
pow(2, 8)
"2 raised to the power 8 is 256"
7) FUNCTION WITH RETURN VALUE:
check = function(x) {
if (x > 0)
{
result = "Positive"
}
else
if (x < 0)
{
result = "Negative"
}
else
{
result = "Zero"
}
return(result)
}
check(x=5)
check(x=-2)
check(x=0)
CONCLUSION: Thus, we have performed Grouping Loops and Conditional Execution
operations in R.
LAB OUTCOME:
ITL804.1: - Install and use R for simple programming tasks
ITL804.4: - Code statistical functions in R
EXPERIMENT NO. - 4
Lab Outcome: ITL804.1: - Install and use R for simple programming tasks
ITL804.4: - Code statistical functions in R
Practical Incharge
EXPERIMENT NO. 4
THEORY:
1) Create vectors,
2) put the vectors into a data frame
3) Perform operation on the data frame
4) We can save the data frame into a file
5) Import of the file into R
6) Examine the data frames that came with R
Example:
Y <- c(2012,2004,2000,1980,1965,2008)
captaincy$names
captaincy$won
captaincy$played
print(ratio)
View(captaincy)
options(digits=2)
mean(captaincy$played)
(45+49+47+47+40+25) / 6
plot(captaincy$Y,ratio)
library(ggplot2)
geom_bar(stat='identity')
read.table((file = "data1.csv"))
data()
View(CO2)
Output:
Create Dataset:
Convert Dataset into Dataframe:
Barplot Graph:
Create New Excel File to store data:
LAB OUTCOME:
ITL804.1: - Install and use R for simple programming tasks
ITL804.4: - Code statistical functions in R
EXPERIMENT NO. - 5
Practical Incharge
EXPERIMENT NO. 5
THEORY:
Graphics in R
1) Bar Plot
barplot(max.temp)
2) Table
3) Histogram
str(airquality)
Temperature <- airquality$Temp
hist(Temperature)
hist(Temperature, main="Maximum daily temperature at La Guardia Airport", xlab="Temperature
in degrees Fahrenheit", xlim=c(50,100), col="darkmagenta", freq=FALSE )
4) Pie Chart
e=(c("Housing","Food","Cloths","Entertainment","Other"))
v=(c(600,300,150,100,200))
pie(v,labels=e,main="pie chart")
pct=round(v/sum(v)*100)
e=paste(e,pct)
e=paste(e,"%",sep=" ")
pie(v,labels=e,main="pie chart",col=rainbow(length(e)))
Output:
1. Bar Plot
max.temp = c(22,27,26,24,23,26,28)
barplot(max.temp)
# barchart with added parameters
barplot(max.temp,main = "Maximum Temperature In A Week", xlab ="Degree Celcius",
ylab = "Day",names.arg = c("Sun","Mon","Tue","Wed","Thur","Fri","Sat"), col = "darkred",
horiz = TRUE)
2. Table
age = c (17,18,18,17,18,19,18,16,18,18)
table(age)
barplot(table(age), main = "Age Count of 10 Students", xlab = "Age", ylab = "Count",
border= "black", col= "red", density=10)
3. Histogram
str(airquality)
Temperature = airquality$Temp
hist(Temperature)
hist(Temperature, main = "Maximum daily temperature at La Guardia Airport",
xlab= "Temperature in degrees Fahrenheit", xlim=c(50,100), col= "darkmagenta",
freq=FALSE )
4. Pie Chart
e=(c("Housing","Food","Cloths","Entertainment","Other"))
v=(c(600,300,150,100,200))
pie(v,labels=e,main="pie chart")
pct=round(v/sum(v)*100)
e=paste(e,pct)
e=paste(e,"%",sep=" ")
pie(v,labels=e,main="pie chart",col=rainbow(length(e)))
CONCLUSION: Thus, we have performed Grouping Loops and Conditional Execution
operations in R.
LAB OUTCOME:
Practical Incharge
EXPERIMENT NO. 6
AIM: Regression and Correlation
THEORY:
Regression
1) Linear Regression
y=mx+c
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# Apply the lm() function.
relation <- lm(y~x)
print(relation)
# Find weight of a person with height 170.
a <- data.frame(x = 170)
result <- predict(relation,a)
print(result)
2) Plotting
# Give the chart file a name.
png(file = "linearregression.png")
# Plot the chart.
plot(y,x,col = "blue",main = "Height & Weight Regression", abline(lm(x~y)),cex = 1.3,pch = 16,xlab =
"Weight in Kg",ylab = "Height in cm")
# Save the file.
dev.off()
3) Multiple Regression:
y = a + b1x1 + b2x2 +...bnxn
input <- mtcars[,c("mpg","disp","hp","wt")]
# Create the relationship model.
model <- lm(mpg~disp+hp+wt, data = input)
# Show the model.
print(model)
# Get the Intercept and coefficients as vector elements.
cat("# # # # The Coefficient Values # # # ","\n")
a <- coef(model)
print(a)
Xdisp <- coef(model)
Xhp <- coef(model)
Xwt <- coef(model)
print(Xdisp)
print(Xhp)
print(Xwt)
OUTPUT:
1) Linear Regression
x = c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y = c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# Apply the lm() function.
relation = lm(y~x)
print(relation)
# Find weight of a person with height 170.
a = data.frame(x = 170)
result = predict(relation,a)
print(result)
plot(x,y,main="relation")
abline(mod, col = 2,lwd = 3)
2) Plotting
plot(y,x, main="Height & Weight Regressioh", xlab="Weight in Kg", ylab="Height in cm",col =
"blue",abline(lm(x~y)),cex = 1.3,pch = 16)
3) Multiple Regression:
input = mtcars[c("mpg","disp","hp","wt")]
# Create the relationship model.
model = lm(mpg~disp+hp+wt, data = input)
# Show the model.
print(model)
plot(model)
# Get the Intercept and coefficients as vector elements.
cat("# # # # The Coefficient Values # # #","\n")
a = coef(model)
print(a)
Xdisp = coef(model)
Xhp = coef(model)
Xwt = coef(model)
print(Xdisp)
print(Xhp)
print(Xwt)
CONCLUSION: Thus, we have performed Grouping Loops and Conditional Execution
operations in R.
LAB OUTCOME:
Practical Incharge
EXPERIMENT NO. – 7
AIM: Mini Project on Data Science (Movie Recommendation System)
CODE:
library(recommenderlab)
library(ggplot2)
library(data.table)
library(reshape2)
setwd("C:/Users/91986/OneDrive/Desktop/FINAL YEAR/Submission/R")
movie_data <- read.csv("movies.csv",stringsAsFactors=FALSE)
rating_data <- read.csv("ratings.csv")
str(movie_data)
head(movie_data)
head(rating_data)
recommendation_model$IBCF_realRatingMatrix$parameters
library(ggplot2)
movie_views <- colCounts(ratingMatrix) # count views for each movie
table_views <- data.frame(movie = names(movie_views),
views = movie_views) # create dataframe of views
table_views <- table_views[order(table_views$views,
decreasing = TRUE), ] # sort by number of views
table_views$title <- NA
for (index in 1:10325){
table_views[index,3] <- as.character(subset(movie_data,
movie_data$movieId == table_views[index,1])$title)
}
table_views[1:6,]
image(ratingMatrix[1:20, 1:25], axes = FALSE, main = "Heatmap of the first 25 rows and 25 columns")
OUTPUT:
CONCLUSION: Thus, we have implemented mini-project on movie recommendation
system for data science in R.
LAB OUTCOME:
ITL804.5: - Use R Graphics and Tables to visualize results of various statistical operations on data
ITL804.6: - Apply the knowledge of R gained to data Analytics for real life applications.
--END--