R PROGRAMMING
MOHD FIKRI HADRAWI
Centre Of Statistics And Decision Science Studies
Faculty Of Computer And Mathematical Sciences
UiTM Shah Alam, Selangor
Email: fikri@fskm.uitm.edu.my
Phone: 016-815 0306/03-5543 5507
Office: S3-13, CS1, FSKM
INTRODUCTION TO R
What is R??
R is a language and environment for statistical computing and graphics.
R provides a wide variety of statistical (linear and nonlinear modelling,
classical statistical tests, time-series analysis, classification,
clustering, …) and graphical techniques, and is highly extensible.
It is very flexible and highly customizable. Excellent graphical tools
make R an ideal environment for EDA (Exploratory Data Analysis).
Based on S language
To install R:
1. R-GUI : http://cran.r-project.org/
2. R-Studio : https://www.rstudio.com/products/rstudio/download/#download
For more details: Go to https://www.r-project.org/about.html
WHY R??
Academia
R is a very popular language in academia. Many researchers and scholars use R for experimenting
with data science
Data wrangling
Data wrangling is the process of cleaning messy and complex data sets to enable convenient
consumption and further analysis.
Data visualization
R has many tools that can help in data visualization, analysis, and representation.
Specificity
All the R libraries focus on making one thing certain - to make data analysis easier, more
approachable and detailed.
Machine Learning
R provides ample tools to developers to train and evaluate an algorithm and predict future
events.
Availability
R programming language is open source
R VS PHYTON
Parameter R Programming Language Phyton Programming Language
History Creator: Ross Ihaka and Robert Creator: Guido Van Rossum
Gentleman
User Academician and researchers Programmer and developer
Purpose R focuses on better, user Phyton focus on productivity and
friendly data analysis, code readability
statistics, and graphical
models.
Flexibility All statistical analysis readily Easy to construct new models from
available in R Library scratch. I.e., matrix computation
and optimization
Learning Can be easily learn advanced Consider a good language for
programming starting programmers
R CONSOLE
The console window (in
RStudio, the bottom left
panel) is the place where
R is waiting for you to
tell it what to do, and
where it will show the
results of a command.
The symbol > is the R
prompt.
The symbol + indicates
incomplete command
R SCRIPT EDITOR
PRINTING, COPY, AND SAVING YOUR WORKS
You can print directly from the R console by selecting “Print…” in
the File menu, but this will capture everything (including errors)
from your session.
Alternatively, you can copy what you need and paste it into a word
processor or text editor (suggestion: use “Courier New” font so that
the formatting is identical to the R Console).
In addition, you can save everything in the R Console by using the
“Save to File…”.
OPENING SAVED EDITOR & WORKSPACE HISTORY
Opening Save Editor
Open R File open script
Opening saved workspace
Open R File load workspace
Open Workspace Histroy
Open R history(max.show =
Inf) Run
LOADING R PACKAGE
INVOKING R
Creating and removing objects.
objects() #list the names of all objects
rm() #remove the object from the current environment
rm(list=ls()) #to remove ALL objects from the current enviroment
c (for concatenate) is a built-in function used to combine elements,
NEVER ASSIGN AN OBJECT TO c!
q() #to quit R
HELPS IN R
help() to get help from R.
CONT…
help.start() to get help from R for a beginner.
OBJECT/DATA TYPE/VARIABLE IN R
Numeric
Store floating point (real number)
Can be categorized as Quantitative Variable
Integer (Numeric Data)
Fraction, decimal, complex number
Boolean
TRUE or FASLE
Can be categorized as Qualitative Variable
(Non-Numeric Data)
String
Non-numeric (character)
Missing values
Na or NaN
TYPE OF DATA/OBJECT
to check for type of data in R, use the following function:
typeof() # to check type of data/object
is.numeric()# to check if the data is numeric
is.na() # to check missing data/values
is.character() # to check if the data is character/non-numeric
is.character(“10”)# to check if the data is character/non-numeric
str() # to check the structure of the data
mode()# the check the mode/type of the data
OPERATORS IN R
There are four (4) operators in R language:
R - OPERATORS
Arithmetic Relations Logical Assignment
<- < ! +
-> > & -
= <= && *
>= | /
== || ^
!= %%
%/%
ASSIGNMENT OPERATORS
There are 3 ways to assign an object in R: <- , = , ->
d <- 10
a = 15
10 -> s
ARITHMETIC’S OPERATORS
X = 5
Y = 16
Operation R-output Notes
X+Y 21 To do addition
X-Y To do substraction
X*Y To do
Y/X To do
Y%%X To do
Y%/%X To do
RELATIONAL OPERATORS
X = 5
Y = 16
Operators Notes Answer
X < Y X less than Y (TRUE / FALSE)
X > Y X greater than Y (TRUE / FALSE)
X <= 5 (TRUE / FALSE)
Y >= 20 (TRUE / FALSE)
Y == 16 (TRUE / FALSE)
X != 5 (TRUE / FALSE)
LOGICAL OPERATORS
k = c(TRUE,FALSE,0,6)
m = c(FALSE,TRUE,FALSE,TRUE)
Operator R-output Notes
!k FALSE TRUE TRUE FALSE the compliment of k
k&m k AND m (the intersection of k and m)
k&&m
k|m
k||m
R AS A CALCULATOR
Operation R-output Notes
abs(3-6) 3 abs() – to find absolute value
sqrt(16) sqrt() – to find square root value
3^10
exp(1.7)
log(10)
log10(100)
pi 3.141593
round(pi,2)
floor(14.7)
ceiling(14.7)
END