VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELAGAVI
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Accredited by NBA
LAB MANUAL
COURSE NAME: R Programming
COURSE CODE: BCS358B
SEMESTER III
2023-24
PROGRAM OUTCOME’S (PO's)
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems
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
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling 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
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
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
10. 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
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
INSTITUTION VISION STATEMENT and MISSION STATEMENT
VISION STATEMENT
To impart Quality Education with Human values and emerge as one of the Nation’s
leading Institutions in the field of Technical Education and Research.
MISSION STATEMENTS
Strive to encourage ideas, talents and value systems.
Guide students to be successful in their endeavor with moral and ethical values.
Build relation with Industries and National Laboratories to support in the field of
Engineering and Technology.
Inculcate a thirst for knowledge in students and help them to achieve Academic
Excellence and Placement.
Train and develop the faculty to achieve Professional,Organizational objectives, and
excel in Research and Development
VISION STATEMENT and MISSION STATEMENTS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VISION STATEMENT
"To strive for excellence in imparting knowledge of Computer Science and Engineering to
produce IT professionals committed to human values".
MISSION STATEMENTS
Mission 1: Impart quality education in cutting edge technologies to achieve excellence in
computer science and engineering to solve real-world problems.
Mission 2: Imbibe human values and ethical responsibilities in professional endeavors.
PROGRAM SPECIFIC OUTCOMES:
PSO1: To analyze and resolve the engineering problems related to Artificial Intelligence and Big
Data analytics for efficient design of a computer-based system of varying complexity.
PSO2: To design, develop, and arrive at the optimal solution for complex computer science
engineering problems with synthesized optimal hardware and software.
PSO3:Apply reasoning informed by the contextual knowledge of computer science and
engineering to resolve societal, health, safety and environmental problems.
PROGRAM EDUCATIONAL OBJECTIVEs (PEOs)
PEO1: To prepare graduates to succeed in IT-enabled professional careers, higher studies and
research by providing a contextually appropriate academic environment.
PEO2: To prepare graduates to be independent and adapt to the changing technologies by
inculcating life-long learning ability, leadership qualities and entrepreneurial skills.
PEO3: To prepare graduates to be committed citizens with social, ethical, and professional
concerns.
Introduction toRprogramming:
R is a programming language and free software developed by Ross Ihaka and Robert
Gentleman in1993. R possesses an extensive catalog of statistical and graphical
methods. It includes machinelearning algorithms, linear regression, time series,
statistical inference to name a few. Most of the Rlibraries
arewritteninR,butforheavycomputational tasks,C,C++andFortran codesarepreferred. R is
not only entrusted by academic, but many large companies also use R
programminglanguage,includingUber,Google,Airbnb,Facebookandsoon.
DataanalysiswithRisdoneinaseriesofsteps;programming,transforming,discovering,modeli
ngandcommunicatetheresults.
Program:Risaclearandaccessibleprogrammingtool
Transform: Ris madeupofacollectionoflibrariesdesignedspecificallyfordatascience
Discover:Investigatethedata, refineyour hypothesisandanalyzethem
Model:Rprovidesawidearrayoftoolsto capturetherightmodelforyourdata
Communicate: Integrate codes, graphs, and outputs to a report with R Markdown or
build Shinyappstosharewiththeworld
What isRusedfor?
Statisticalinferen
ceDataanalysis
Machinelearningalgorithm
InstallationofR-Studioonwindows:
Step – 1: With R-base installed, let’s move on to installing R Studio. To begin, gotodownload
RStudioand clickonthe download buttonforRStudiodesktop.
Step–2:Clickonthelink forthewindowsversionofRStudioand
savethe.exefile.Step–3:Runthe .exe andfollowthe installationinstructions.
3. ClickNext onthewelcome window.
Enter/browsethepathtotheinstallation folderandclickNextto proceed.
Select the folder for the start menu shortcut or click on do not create shortcuts
and thenclickNext.
Waitfortheinstallationprocesstocomplete.
ClickFinishtoendtheinstallation.
InstalltheRPackages: -
InRStudio,ifyourequireaparticularlibrary,thenyoucangothroughthefollowinginstruct
ions:
First,runRStudio.
After clicking on the packages tab, click on install. The following dialog box
willappear.
In the Install Packages dialog, write the package name you want to
install underthe Packagesfield andthen clickinstall.Thiswillinstall the
packageyousearchedfororgiveyoualistofmatchingpackagesbasedonyour
packagetext.
InstallingPackages: -
ThemostcommonplacetogetpackagesfromisCRAN.ToinstallpackagesfromCRANyouuseinstall
. Packages ("packagename").Forinstance,ifyouwanttoinstalltheggplot2package, which is a
very popular visualization package, you would type the following in theconsole: -
Syntax: -
#installpackagefromCRAN
install.
Packages("ggplot2")Loadin
gPackages: -
Oncethepackageisdownloadedtoyourcomputer youcanaccessthefunctionsand
resourcesprovidedbythepackageintwodifferentways:
#loadthepackagetouseinthecurrentRsession
library (package name)
AssignmentOperators: -
Thefirstoperatoryou’llrunintoistheassignmentoperator.Theassignmentoperatorisusedto assign a
value. For instance, we can assign the value 3 to the variable x using the <-
assignmentoperator.
# assignmentx<-3
Interestingly,Ractuallyallows forfiveassignmentoperators:
#leftwardassignment
x<-valuex=value
x<<-value
#
rightward assignment
value -> xvalue->>x
TheoriginalassignmentoperatorinRwas<-andhascontinuedtobethepreferredamongRusers. The =
assignment operator was added in 2001 primarily because it is the acceptedassignment
operator in many other languages and beginners to R coming from otherlanguages
weresopronetouseit.
Theoperators<<- isnormallyonlyusedinfunctionswhichwewillnotgetintothedetails.
Evaluation
Wecanthenevaluatethevariablebysimplytypingxat thecommandline
whichwillreturnthevalueofx.Notethat priortothevaluereturnedyou’llsee## [1]inthecommandline.
This simply implies that the output returned is the first output. Note that you can type
anycommentsinyour
codebyprecedingthecommentwiththehashtag(#)symbol.Anyvalues,symbols,andtexts
following#willnotbe evaluated.
# evaluationx
##[1] 3
CaseSensitivity
Lastly,notethatRisacasesensitiveprogramminglanguage.Meaningallvariables,functions,andobjec
tsmustbe calledbytheirexactspelling:
x<-1
y<-3
z <-4
x * y * z##[1]12
x*Y*z
##Errorineval(expr,envir,enclos): object'Y'notfound
BasicArithmetic
AtitsmostbasicfunctionRcanbeusedasacalculator.Whenapplyingbasicarithmetic, thePEMDAS
order of operations applies: parentheses first followed by
exponentiation,multiplicationanddivision,andfinaladditionandsubtraction.
8+9/5^ 2
R Programming Lab(BCS358B)
##[1]8.36
8+9/(5^2)
##[1]8.36
8+(9/5)^ 2
##[1]11.24
(8+9)/5^ 2
##[1]0.68
BydefaultRwilldisplaysevendigitsbut thiscanbechangedusing options
()aspreviouslyoutlined.
1/7
##[1]0.1428571
options (digits=3)
1/7
##[1]0.143
pi
##[1]3.141592654
options (digits = 22)pi
##[1]3.141592653589793115998
We can also perform integer divide (%/%) and modulo (%%) functions. The integer
dividefunctionwillgivetheintegerpartofafractionwhilethemodulowillprovidetheremainder.42/
4 #regulardivision
##[1]10.5
42%/%4 #integerdivision
##[1] 10
42%%4 #modulo(remainder)
##[1] 2
MiscellaneousMathematicalFunctions
Therearemanybuilt-
infunctionstobeawareof.Theseincludebutarenotlimitedtothefollowing.Goaheadandrunthisco
deinyourconsole.
x<-10
abs(x) #absolutevaluesqrt(x) #squareroot
exp(x) #exponentialtransformationlog(x) #logarithmictransformation
cos(x) #cosineandothertrigonometricfunctions
Head: -
Tobegin,wearegoing
toruntheheadfunction,whichallowsustoseethefirst6rowsbydefault.Wearegoingtooverridethe
defaultandasktopreviewthefirst10rows.
>head (df,10)
Tail: -Tailfunctionallowsustoseethelastnobservations fromagivendataframe.
Thedefaultvaluefornis6.Usercanspecifyvalueofnasperas requirements.
>tail(mtcars,n=5)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
DimandGlimpse
Next,wewillrunthedimfunctionwhichdisplaysthedimensionsofthetable.Theoutputtakes the
form of row, column. And then we run the glimpse function from the plypackage. This will
display a vertical preview of the dataset. It allows us to easily
previewdatatypeandsampledata.
1.Demonstrate the steps for installation of R and R Studio. Perform the
following:
a) Assign different type of values to variables and display the type of variable.
Assign different types
such as Double, Integer, Logical, Complex and Character and understand the
difference between each data type.
b) Demonstrate Arithmetic and Logical Operations with simple examples.
c) Demonstrate generation of sequences and creation of vectors.
d) Demonstrate Creation of Matrices
e) Demonstrate the Creation of Matrices from Vectors using Binding
Function.
f) Demonstrate element extraction from vectors, matrices and arrays
Data Types in R:
# Double
double_var<- 3.14
print(class(double_var))
# Integer
int_var<- 42L
print(class(int_var))
# Logical
logical_var<- TRUE
print(class(logical_var))
# Complex
complex_var<- 2 + 3i
print(class(complex_var))
# Character
char_var<- "Hello, World!"
print(class(char_var))
Arithmetic Operators in R:
Operator Description
+ Addition
- Subtraction
* Multiplication
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
/ Division
^ or ** Exponentiation
%% Modulo Division
(remainder of division)
%/% Integer Division
Logical Operators in R:
Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are
TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement
is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT - returns FALSE if statement is TRUE
Vectors in R:
• In R programming, a vector is the most common data structure. It is an array of
data elements, each the same type (integer, double, character, logical, or
complex).
• Vectors can be atomic, also called scalar variable.
• The function c(), which stands for concatenate, is useful for creating vectors.
• Another useful function for creating vectors is the seq() function, which
generates sequences.
– seq(start, end)
– start:stop
• We can name the elements of a numeric vector using the names() function.
Matrices in R:
• A matrix is a two dimensional data set with columns and rows.
• A column is a vertical representation of data, while a row is a horizontal
representation of data.
– matrix(data, nrow, ncol, byrow)
– cbind(v1, v2, v3, …)
• Combines vectors by columns
– rbind(v1, v2, v3, …)
• Combines vectors by rows
Element extraction:
• We use square brackets for subsetting to access specific elements of a vector or
matrix.
• If the entries of a vector are named, they may be accessed by referring to their
name.
• We get the number of elements using length() function.
EX 2:Assess the Financial Statement of an Organization being supplied with 2
vectors of data: Monthly Revenue
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
and Monthly Expenses for the Financial Year. You can create your own
sample data vector for this
experiment) Calculate the following financial metrics:
a. Profit for each month.
b. Profit after tax for each month (Tax Rate is 30%).
c. Profit margin for each month equals to profit after tax divided by revenue.
d. Good Months – where the profit after tax was greater than the mean for the
year.
e. Bad Months – where the profit after tax was less than the mean for the
year.
f. The best month – where the profit after tax was max for the year.
g. The worst month – where the profit after tax was min for the year.
Note:
a. All Results need to be presented as vectors
b. Results for Dollar values need to be calculated with $0.01 precision, but
need to be presented in
Units of $1000 (i.e 1k) with no decimal points
c. Results for the profit margin ratio need to be presented in units of % with
no decimal point.
d. It is okay for tax to be negative for any given month (deferred tax asset)
e. Generate CSV file for the data.
Suggested Reading –
Source Code:
data <- read.csv("data.csv")
revenue <- c(data$revenue)
expenses <- c(data$expenses)
profits <- revenue - expenses
print("Profits for each month")
for(i in 1:12) {
cat("Profit for month", i, "is", profits[i], "\n")
}
profit_after_tax<- profits - round(profits * 0.3, 2)
for(i in 1:12) {
cat("Profit after tax for month", i, "is", profit_after_tax[i], "\n")
}
profit_margin<- round(profit_after_tax/revenue, 2) * 100
for(i in 1:12) {
cat("Profit margin for month", i, "is", paste(profit_margin[i], "%"), "\n")
}
mean_for_year<- mean(profit_after_tax)
good_months = c()
bad_months = c()
for(i in 1: length(profit_after_tax)) {
if(profit_after_tax[i] >mean_for_year)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
good_months = c(good_months, i)
if(profit_after_tax[i] <mean_for_year)
bad_months = c(bad_months, i)
}
cat("Following months had profit after tax higher than mean:", good_months)
cat("Following months had profit after tax lower than mean:", bad_months)
max_profit = max(profit_after_tax)
min_profit = min(profit_after_tax)
cat("Month where profit after tax is highest is", which(TRUE == (profit_after_tax ==
max_profit)))
cat("Month where profit after tax is lowest is", which(TRUE == (profit_after_tax ==
min_profit))
EX 3: Develop a program to create two 3 X 3 matrices A and B and perform
the following operations
a) Transpose of the matrix b) addition c) subtraction d) multiplication
# create matrix with 3 rows and 3 columns
Matrix = matrix(1:9, nrow = 3)
# print the matrix
print(Matrix)
# create another matrix
M2 = Matrix
# Loops for Matrix Transpose
for (i in 1:nrow(M2))
{
# iterate over each row
for (j in 1:ncol(M2))
{
# iterate over each column
# assign the correspondent elements
# from row to column and column to row.
M2[i, j] <- Matrix[j, i]
}
}
# print the transposed matrix
print(M2)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Output:
[, 1] [, 2] [, 3]
[1,] 1 3 5
[2,] 2 4 6
[, 1] [, 2]
[1, ] 1 2
[2, ] 3 4
[3, ] 5 6
EX 4: Develop a program to find the factorial of given number using recursive
function calls.
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 5: Develop an R Program using functions to find all the prime numbers up
to a specified number by the
method of Sieve of Eratosthenes
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 6: The built-in data set mammals contain data on body weight versus
brain weight. Develop R commands to:
a) Find the Pearson and Spearman correlation coefficients. Are they similar?
b) Plot the data using the plot command.
c) Plot the logarithm (log) of each variable and see if that makes a difference
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 7:
EX 8: Develop R program to create a Data Frame with following details and
do the following operations.
item Code Item Category Item Price
1001 Electronics 700
1002 Desktop Supplies 300
1003 Office Supplies 350
1004 USB 400
1005 CD Drive 800
a) Subset the Data frame and display the details of only those items whose
price is greater than or equal to 350.
b) Subset the Data frame and display only the items where the category is
either “Office Supplies” or “Desktop Supplies”
c) Create another Data Frame called “item-details” with three different fields
item Code, Item QtyonHand and ItemReorderLvl and merge the two frames
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 9: Design a data frame in R for storing about 20 employee details. Create
a CSV file named “input.csv” that defines all the required information about
the employee such as id, name, salary, start date, dept. Import into R and do
the following analysis.
a) Find the total number rows & columns
b) Find the maximum salary
c) Retrieve the details of the employee with maximum salary
d) Retrieve all the employees working in the IT Department.
e) Retrieve the employees in the IT Department whose salary is greater than
20000 and write these details into another file “output.csv”
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 11: Using the built in dataset mtcars which is a popular dataset consisting
of the design and fuel consumption patterns of 32 different automobiles. The
data was extracted from the 1974 Motor Trend US magazine, and comprises
fuel consumption and 10 aspects of automobile design and performance for 32
automobiles (1973-74 models). Format A data frame with 32 observations on
11 variables : [1] mpg Miles/(US) gallon, [2] cyl Number of cylinders [3] disp
Displacement (cu.in.), [4] hp Gross horsepower [5] drat Rear axle ratio,[6] wt
Weight (lb/1000) [7] qsec 1/4 mile time, [8] vs V/S, [9] am Transmission (0 =
automatic, 1 = manual), [10] gear Number of forward gears, [11] carb
Number of carburetors
Develop R program, to solve the following:
a) What is the total number of observations and variables in the dataset?
b) Find the car with the largest hp and the least hp using suitable functions
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
c) Plot histogram / density for each variable and determine whether
continuous variables are normally distributed or not. If not, what is their
skewness?
d) What is the average difference of gross horse power(hp) between
automobiles with 3 and 4 number of cylinders(cyl)? Also determine the
difference in their standard deviations.
e) Which pair of variables has the highest Pearson
correlation?
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
EX 11: Demonstrate the progression of salary with years of experience using a
suitable data set (You can create your own dataset). Plot the graph visualizing
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
the best fit line on the plot of the given data points. Plot a curve of Actual
Values vs. Predicted values to show their correlation and performance of the
model.
Interpret the meaning of the slope and y-intercept of the line with respect to
the given data. Implement using lm function. Save the graphs and coefficients
in files. Attach the predicted values of salaries as a new column to the original
data set and save the data as a new CSV file.
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
Department of Computer Science and Engineering, SGBIT, Belagavi
R Programming Lab(BCS358B)
REFERENCES
1. . Cotton, R. (2013). Learning R: A Step by Step Function Guide to Data
Analysis. 1st ed. O’Reilly Media Inc.
2. Jones, O., Maillardet. R. and Robinson, A. (2014). Introduction to
Scientific Programming and Simulation Using R. Chapman & Hall/CRC,
The R Series.
3. 2Davies, T.M. (2016) The Book of R: A First Course in Programming and
Statistics. No Starch Press.
Department of Computer Science and Engineering, SGBIT, Belagavi