R Programming Lab Manual
R Programming Lab Manual
LAB MANUAL
COURSE NAME: R Programming
COURSE CODE: BCS358B
SEMESTER III
2023-24
PROGRAM OUTCOME’S (PO's)
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
VISION STATEMENT
"To strive for excellence in imparting knowledge of Computer Science and Engineering to
produce IT professionals committed to human values".
MISSION STATEMENTS
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.
PEO1: To prepare graduates to succeed in IT-enabled professional careers, higher studies and
research by providing a contextually appropriate academic environment.
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
Model:Rprovidesawidearrayoftoolsto capturetherightmodelforyourdata
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
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(mtcars,n=5)
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.
Arithmetic Operators in R:
Operator Description
+ Addition
- Subtraction
* Multiplication
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.
Source Code:
[, 1] [, 2] [, 3]
[1,] 1 3 5
[2,] 2 4 6
[, 1] [, 2]
[1, ] 1 2
[2, ] 3 4
[3, ] 5 6
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
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.