Introduction to R Programming
Dr. Mohammad Nasir Abdullah
PhD(Statistics), MSc (Medical Statistics), BSc(hons) (Statistics), Diploma in Statistics, Certified Data Science
Specialist, Graduate Statistician (Royal Statistical Society)
Senior Lecturer,
Mathematical Sciences Studies,
College of Computing, Informatic and Media,
Universiti Teknologi MARA,
Perak Branch, Tapah Campus, Perak.
• https://nasirdrive1.wixsite.com/nasir916
Welcome Pack
• https://tinyurl.com/y2uo4m7x <- please fill in your email address
• https://tinyurl.com/IntroRUCS <- by using email address above, please
register Rstudio Cloud for hands on material.
What is R?
R is a language and environment for statistical
computing and graphics.
R is an open-source implementation of the S
language, which was developed by Bell laboratories.
R is both open source and open development
(http://www.r-project.org/)
Watch this!
What R Does R Does Not…
• Data handling and storage: • is not a database, but connects to
numeric, textual DBMSs
•matrix algebra • has no graphical user
•Hash tables and regular interfaces, but connects to
Java, TclTk
expressions
• language interpreter can be
• high-level data analytic and very slow, but allows to call
statistical functions own C/C++ code
• classes (“OO”) • no spreadsheet view of data,
• graphics but connects to Excel/MsOffice
• programming language: loops, • no professional / commercial
branching, subroutines support
Advantages using R Disadvantages using R
•Fast and free. •Not user friendly @ start - steep
•R is state of the art: SPSS and SAS learning curve, minimal GUI.
are years behind R! •No commercial support; figuring
• 2nd only to MATLAB for graphics. out correct methods or how to use a
function on your own can be
•Mx, WinBugs, and other programs frustrating.
use or will use R.
•Easy to make mistakes and not
•Active user community know.
•Excellent for simulation, •Working with large datasets is
programming, computer intensive limited by RAM
analyses etc. •Data prep & cleaning can be
•Forces you to think about your messier & more mistake prone in R
analysis. vs. SPSS or SAS
•Interfaces with database •Some users complain about
storage software (SQL) hostility on the R listserve
R is exploding in popularity
Google Trends
“R is the most powerful & flexible statistical
e ol tio nfidential
programming language in the world”1
Capabilities
Sophisticated
statistical analyses
Predictive analytics
Data visualization
Applications
Real-time trading MSFT [2009-
Last 29.29
Finance 30
Risk assessment 25
Forecasting 20
Bio-technology 15
Drug development
Social networks
.. and more
7
From: The R Ecosystem
R User Community bit.ly/R-ecosystem
8
R has a Steep Learning Curve
(steeper for those that knew SAS or other software before)
Basic commands
Interface
Data types
Packages
Procedures
12
R History
R is a comprehensive statistical and graphical
programming language and is a dialect of the S
language:
1988 - S2: RA Becker, JM Chambers, A Wilks 1992 - S3: JM
Chambers, TJ Hastie
1998 - S4: JM Chambers
R: initially written by Ross Ihaka and Robert Gentleman
at Dep. of Statistics of U of Auckland, New Zealand
during 1990s.
He retired as an associate professor of statistics at the University of Auckland
Since 1997: international “R-core” team of 15 people
with access to common CVS archive.
15
Installing R
Install the latest Latest version : 4.3.0
version from :
http://cran.r-project.org/
Install RStudio:
https://posit.co/
RSTUDIO-2023.03.1-446.EXE
https://posit.co/download/rstudio-desktop/#:~:text=RSTUDIO
%2D2023.03.1%2D446.EXE
There are over 15000 add-
R Packages on packages
To date: 19,601 available packages
(31st May 2023)
The most important single innovation in R is the package system, which
provides a cross-platform system for distributing and testing code and data.
The Comprehensive R Archive Network (http://cran.r-project.org) distributes
public packages, but packages are also useful for internal distribution.
A particular R
strength:
genetics
• Bioconductor is a suite of
additional functions and some
2,230 packages dedicated to
analysis, visualization, and
management of genetic data
• Much more
functionality than
software released by
Affy or Illumina
The power of Rgraphics
For example : Create beautiful statistical graphics with ggplot2
R has a very powerful graphics system,
With low-level tools allowing customization of every detail
Example of RGraphics
Dashboard
Useful Web links
Datacamp http://www.datacamp.com
UCLA Institute for Digital Research and Education
http://www.ats.ucla.edu/stat/r/
R Reference card:
http://cran.r-project.org/doc/contrib/Short-refcard.pdf
Undergrad Guide to R :
https://sites.google.com/site/undergraduateguidetor/
Quick R: http://statmethods.net/
57
• The R console “interprets”
whatever you type.
Working • Calculator
with R • Creating variables
• Applying functions.
RStudio
• Easier working with R
• Syntax highlighting, code completion and smart
indentation
• Easily manage multiple working directories and projects.
• More information
• Workspace browser and data viewer
• Plot history, zooming and flexible image and PDF export
• Integrated R help and documentation
• Searchable command history
Latest version 2023.03.1+446 May 2023
• Where code is executed
(where things happen)
• You can type here for
things interactively.
• Code is not saved on
your disk
Rstudio Layout
• If Rstudio doesn’t look like
this (then do:)
• Rstudio -> Preferences ->
Pane Layout
Source/Editor
• Where files open to
• Have R code and
comments in them
• Can highlight and press
(CMD+Enter (Mac)) or
Ctrl+Enter (Windows))
to run the code
• In a .R file (we call a
script), code is saved on
your disk
Workspace/Environment
• Tells you what objects are in
R.
• What exists in memory/
what is loaded?/ what did I
read in?
• History
• Shows previous commands.
Good to look at for
debugging, but don’t rely on
it a scripts. Make a script!
• Also type the “up” key in the
console to scroll through
previous commands.
Other panes
• Files
• Shows the files on your computer of the directory you are working
in
• Viewer
• Can view data or R objects
• Help
• Shows help of R command
• Plots
• Pretty pictures
• Packages
• List of R packages that are loaded in memory
Rstudio Shortcuts
• Ctrl + Enter (CMD + Enter on OSX) in your script evaluates that line of
code
• Ctrl + 1 takes you to the script page
• Ctrl + 2 takes you to the console
• http://www.rstudio.com/ide/docs/using/keyboard_shortcuts
Thank you
Email :
[email protected]