SlideShare a Scribd company logo
1 of 37
Download to read offline
An Introduction to R
Programming Language Implementation and Design
PhD. Saied Farzi
Lecture by Mahmoud Shiri Varamini
Amin Khaleghi
K.N.Toosi university of tech.
autumn 2016
shirivaramini@gmail.com
2
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.
●Created in 1993, license: GNU GPL
●Interpreted
●C-like syntax
●Functional programming language semantics (Lisp, APL)
●Object oriented (3 different OO systems)
●Garbage collector
●Mostly call-by-value
●Lexical scope
●Function closure
3
Popularity
● Github: 12
● Stackoverflow: 117341 questions (Java: 978006, Python:
507653)
● Most popular tool for statistical data analysis
4
History
R is an implementation of the S programming language
combined with lexical scoping semantics inspired by Scheme.
S was created by John Chambers while at Bell Labs. There
are some important differences, but much of the code written
for S runs unaltered.
5
History
R was created by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand, and is currently
developed by the R Development Core Team, of which
Chambers is a member. The project was conceived in 1992,
with an initial version released in 1995 and a stable beta
version in 2000.
6
Usage
●Statistics (frequentist and bayesian)
●Machine learning and data mining
●Science (mathematics, chemistry, physics, medical, ecology,
genetics,economy, history, …)
●Finance
●Natural Language Processing
●Data visualization
●Analyzing spatial, spatio-temporal data and time series
●…
7
Applications
Who use R in their business :
8
competitors/colleagues
●SAS, SPSS, STATA, Mathematica and other statistical
software
●Python + Numpy + Pandas + matplotlib + …
●Matlab/Octave
●Julia
●K/J and other APL like languages
●Java (Weka), Clojure, .NET (F#), …
9
Calling R
●command line
●SAS, SPSS, Stata, Statistica, JMP
●Java, C++, F#
●Python, Perl, Ruby, Julia
●PostgreSQL: PL/R
10
General Structure
Workspace
Object
Object
Object
Object
Object
Object
ObjectObject
11
Data Types (Modes)
• Numeric
• Character
• Logical (TRUE / FALSE)
• Complex
• Raw (bytes)
12
Data structure
R is an object-oriented language: an object in R is anything
(constants, data structures, functions, graphs) that can be
assigned to a variable:
● Data Objects: used to store real or complex numerical
values, logical values or characters. These objects are always
vectors: there are no scalars in R.
● Language Objects: functions, expressions
13
Data structure types
Vectors: one-dimensional arrays used to store collection
data of the same mode
●Numeric Vectors (mode: numeric)
●Complex Vectors (mode: complex)
●Logical Vectors (model: logical)
●Character Vector or text strings (mode: character)
Matrices: two-dimensional arrays to store collections of data
of the same mode. They are accessed by two integer indices.
14
Data structure types
Arrays: similar to matrices but they can be multi-dimensional
(more than two dimensions)
Factors: vectors of categorical variables designed to group
the components of another vector with the same size
Lists: ordered collection of objects, where the elements can
be of different types
Data Frames: generalization of matrices where different
columns can store different mode data.
Functions: objects created by the user and reused to make
specific operations.
15
Data structure types
16
Numeric Vectors
There are several ways to assign values to a variable:
> a <- 1.7
> 1.7 -> a
> a = 1.7
> assign("a", 1.7)
To show the values:
> a
[1] 1.7
> print(a)
[1] 1.7
17
Numeric Vectors
To generate a vector with several numeric values:
> a <- c(10, 11, 15, 19)
The operations are always done over all the elements of the
numeric array:
> 1/a
[1] 0.10000000 0.09090909 0.06666667 0.05263158
> b <- a-1
> b
[1] 9 10 14 18
To generate a sequence:
> 2:10
[1] 2 3 4 5 6 7 8 9 10
18
Logical Vectors
a <- seq(1:10)
> a
[1] 1 2 3 4 5 6 7 8 9 10
> b <- (a>5)
> b
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE
TRUE TRUE TRUE
> a[b]
[1] 6 7 8 9 10
> a[a>5]
[1] 6 7 8 9 10
19
Character Vectors
a <- "This is an example"
> a
[1] "This is an example"
We can concatenate vectors after converting them into
character vectors:
> x <- 1.5
> y <- -2.7
> paste("Point is (",x,",",y,")", sep="")
[1] "Point is (1.5,-2.7)"
20
Matrices
A matrix is a bi-dimensional collection of data:
> a <- matrix(1:12, nrow=3, ncol=4)
> a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> dim(a)
[1] 3 4
21
Arrays
They are similar to the matrices although they can have 2 o
more dimensions.
> z <- array(1:24, dim=c(2,3,4))
> z
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
...
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
22
Factors
Factors are vectors that contain categorical information useful
to group the values of other vectors of the same size. Let’s
see an example:
> bv <- c(0.92,0.97,0.87,0.91,0.92,1.04,0.91,0.94,0.96,
+ 0.90,0.96,0.86,0.85) # (B-V) colours
from 13 galaxies
23
Lists
Lists are ordered collections of objects, where the elements
can be of a different type (a list can be a combination of
matrices, vectors, other lists, etc.) They are created using the
list() function:
> gal <- list(name="NGC3379", morf="E", T.RC3=-5,
colours=c(0.53,0.96))
> gal
$name
[1] "NGC3379"
$morf
[1] "E"
$T.RC3
[1] -5
$colours
[1] 0.53 0.96
24
Data Frames (Tables)
A Data Frame is an special type of list very useful for the
statistical work. There are some restrictions to guarantee that
they can be used for this statistical purpose.
Among other restrictions, a Data Frame must verify that:
●List components must be vectors (numeric, character or
logical vectors), factors, numeric matrices or other data
frames.
●Vectors, which are the variables in the data frame, must be
of the same length.
25
Control statements
● Conditional execution : if statements
● Repetitive execution: for loops, repeat and while
26
if statements
The syntax of if statement is:
if (test_expression) {
statement
}
The syntax of if...else statement is:
if (test_expression) {
statement1
} else {
statement2
}
27
for loops
for ( name in expr_1 )
expr_2
expr: expression
Ex:
for (i in 1:10) {
if (!i %% 2){
next
}
print(i)
}
[1] 1 3 5 7 9
28
repeat
repeat {
statement
}
Ex:
x <- 1
repeat {
print(x)
x = x+1
if (x == 6){
break
}
}
[1] 1 2 3 4 5
29
while
while(cond)
expr
cond: condition
expr: expression
Example:
> x <- 1
> while(x < 5) {x <- x+1; print(x);}
[1] 2 3 4
30
Operators
We have the following types of operators in R programming:
● Arithmetic Operators ( + , - , * , / , %% [give the
remainder] , %/% [result of division or quotient] , ^
[exponent] )
● Relational Operators ( > , < , == , <= , >= , != )
● Logical Operators ( & , | , ! , && , || )
● Assignment Operators ( ← or = or < [Called Left
Assignment ] , → or →> [Called Right Assignment] )
●Miscellaneous Operators ( : [creates the series of numbers
in sequence for a vector] , %in% [This operator is used to
identify if an element belongs to a vector] , %*% [multiply a
matrix with its transpose] )
31
read and write
In R, we can read data from files stored outside the R
environment. We can also write data into files which will be
stored and accessed by the operating system. R can read
and write into various file formats like csv, excel, xml etc.
read.table()
Ex:
Mydata ← read.table(“c:/test/data.txt”)
if you want to use back slash you should do this:
Mydata ← read.table(“c:testdata.txt”)
write.table(x, file=” ”)
write.csv2(x, file=”*.csv”)
32
User Interfaces for R
Rstudio Integrated development environment (IDE) for R
Rattle Gnome cross platform GUI for Data Mining using
RRed-R Open source visual programming interface for
Rdeducer Intuitive, cross-platform graphical data analysis
system
RKWard Easy to use, transparent frontend
JGR Universal and unified graphical user interface for
R
R Commander Basic-Statistics GUI for R
terminal Linux terminal
33
Real world scenario :
Mandelbrot set
Short R code calculating Mandelbrot set through the first 20
iterations of equation z = z^2 + c plotted for different complex
constants c. This example demonstrates:
● use of community-developed external libraries (called
packages), in this case caTools package
● handling of complex numbers
● multidimensional arrays of numbers used as basic data
type, see variables C, Z and X.
34
Real world scenario :
Mandelbrot set
install.packages("caTools") # install external package
library(caTools) # external package providing write.gif function
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
"#7FFF7F",
"yellow", "#FF7F00", "red", "#7F0000"))
dx <- 400 # define width
dy <- 400 # define height
C <- complex( real=rep(seq(-2.2, 1.0, length.out=dx), each=dy ),
imag=rep(seq(-1.2, 1.2, length.out=dy), dx ) )
C <- matrix(C,dy,dx) # reshape as square matrix of complex numbers
Z <- 0 # initialize Z to zero
X <- array(0, c(dy,dx,20)) # initialize output 3D array
for (k in 1:20) { # loop with 20 iterations
Z <- Z^2+C # the central difference equation
X[,,k] <- exp(-abs(Z)) # capture results
}
write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=900)
35
Real world scenario :
Mandelbrot set
36
R programmers salary
in USA
37
sources
1)http://ect.bell-labs.com/sl/S/
2)http://adv-r.had.co.nz/Environments.html
3)http://cran.ma.imperial.ac.uk/doc/contrib/Raeesi-SNA_in_R_
in_Farsi.pdf
4)http://venus.ifca.unican.es/Rintro/dataStruct.html
5)https://www.stat.auckland.ac.nz/~paul/ItDT/HTML/node64.h
tml
6)http://www.ahschulz.de/pub/R/data_structures/Data_Struct
ures_in_R_web.pdf
7)https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf
8)https://www.datacamp.com/community/tutorials/tutorial-on-l
oops-in-r#gs.i08v0N0

More Related Content

What's hot

Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 

What's hot (20)

Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
R programming
R programmingR programming
R programming
 
R Programming
R ProgrammingR Programming
R Programming
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
How to get started with R programming
How to get started with R programmingHow to get started with R programming
How to get started with R programming
 
Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
R programming
R programmingR programming
R programming
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
Exploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubExploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science Club
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
Topic Models
Topic ModelsTopic Models
Topic Models
 
Machine Learning in R
Machine Learning in RMachine Learning in R
Machine Learning in R
 
Data visualization with R
Data visualization with RData visualization with R
Data visualization with R
 

Viewers also liked

R kurs so se 2010 uni bremen m. heckmann
R kurs so se 2010 uni bremen   m. heckmannR kurs so se 2010 uni bremen   m. heckmann
R kurs so se 2010 uni bremen m. heckmann
Mark Heckmann
 
Comparing interactive online and face-to-face repertory grid interviews in te...
Comparing interactive online and face-to-face repertory grid interviews in te...Comparing interactive online and face-to-face repertory grid interviews in te...
Comparing interactive online and face-to-face repertory grid interviews in te...
Mark Heckmann
 

Viewers also liked (11)

R-Kurs Mark Heckmann WiSe 2010-2011 Uni Bremen
R-Kurs Mark Heckmann WiSe 2010-2011 Uni BremenR-Kurs Mark Heckmann WiSe 2010-2011 Uni Bremen
R-Kurs Mark Heckmann WiSe 2010-2011 Uni Bremen
 
R base graphics
R base graphicsR base graphics
R base graphics
 
R kurs so se 2010 uni bremen m. heckmann
R kurs so se 2010 uni bremen   m. heckmannR kurs so se 2010 uni bremen   m. heckmann
R kurs so se 2010 uni bremen m. heckmann
 
Comparing interactive online and face-to-face repertory grid interviews in te...
Comparing interactive online and face-to-face repertory grid interviews in te...Comparing interactive online and face-to-face repertory grid interviews in te...
Comparing interactive online and face-to-face repertory grid interviews in te...
 
R Knitr - generating reports
R Knitr - generating reportsR Knitr - generating reports
R Knitr - generating reports
 
The OpenRepGrid project – Software tools for the analysis and administration...
The OpenRepGrid project – Software tools  for the analysis and administration...The OpenRepGrid project – Software tools  for the analysis and administration...
The OpenRepGrid project – Software tools for the analysis and administration...
 
Schleifen in R
Schleifen in RSchleifen in R
Schleifen in R
 
Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009
 
Standardizing inter-element distances in repertory grids
Standardizing inter-element distances in repertory gridsStandardizing inter-element distances in repertory grids
Standardizing inter-element distances in repertory grids
 
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
OpenRepGrid – An Open Source Software for the Analysis of Repertory GridsOpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
 
The Model I Love - Lyrics (performed at Bremen University, Germany, June, 2013)
The Model I Love - Lyrics (performed at Bremen University, Germany, June, 2013)The Model I Love - Lyrics (performed at Bremen University, Germany, June, 2013)
The Model I Love - Lyrics (performed at Bremen University, Germany, June, 2013)
 

Similar to An Intoduction to R

Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 

Similar to An Intoduction to R (20)

R basics
R basicsR basics
R basics
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
Rattle Graphical Interface for R Language
Rattle Graphical Interface for R LanguageRattle Graphical Interface for R Language
Rattle Graphical Interface for R Language
 
Unit 3
Unit 3Unit 3
Unit 3
 
R language introduction
R language introductionR language introduction
R language introduction
 
R Programming - part 1.pdf
R Programming - part 1.pdfR Programming - part 1.pdf
R Programming - part 1.pdf
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 
Starting work with R
Starting work with RStarting work with R
Starting work with R
 
Automatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELAutomatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSEL
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Lecture_R.ppt
Lecture_R.pptLecture_R.ppt
Lecture_R.ppt
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 

Recently uploaded

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Recently uploaded (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

An Intoduction to R

  • 1. An Introduction to R Programming Language Implementation and Design PhD. Saied Farzi Lecture by Mahmoud Shiri Varamini Amin Khaleghi K.N.Toosi university of tech. autumn 2016 shirivaramini@gmail.com
  • 2. 2 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. ●Created in 1993, license: GNU GPL ●Interpreted ●C-like syntax ●Functional programming language semantics (Lisp, APL) ●Object oriented (3 different OO systems) ●Garbage collector ●Mostly call-by-value ●Lexical scope ●Function closure
  • 3. 3 Popularity ● Github: 12 ● Stackoverflow: 117341 questions (Java: 978006, Python: 507653) ● Most popular tool for statistical data analysis
  • 4. 4 History R is an implementation of the S programming language combined with lexical scoping semantics inspired by Scheme. S was created by John Chambers while at Bell Labs. There are some important differences, but much of the code written for S runs unaltered.
  • 5. 5 History R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team, of which Chambers is a member. The project was conceived in 1992, with an initial version released in 1995 and a stable beta version in 2000.
  • 6. 6 Usage ●Statistics (frequentist and bayesian) ●Machine learning and data mining ●Science (mathematics, chemistry, physics, medical, ecology, genetics,economy, history, …) ●Finance ●Natural Language Processing ●Data visualization ●Analyzing spatial, spatio-temporal data and time series ●…
  • 7. 7 Applications Who use R in their business :
  • 8. 8 competitors/colleagues ●SAS, SPSS, STATA, Mathematica and other statistical software ●Python + Numpy + Pandas + matplotlib + … ●Matlab/Octave ●Julia ●K/J and other APL like languages ●Java (Weka), Clojure, .NET (F#), …
  • 9. 9 Calling R ●command line ●SAS, SPSS, Stata, Statistica, JMP ●Java, C++, F# ●Python, Perl, Ruby, Julia ●PostgreSQL: PL/R
  • 11. 11 Data Types (Modes) • Numeric • Character • Logical (TRUE / FALSE) • Complex • Raw (bytes)
  • 12. 12 Data structure R is an object-oriented language: an object in R is anything (constants, data structures, functions, graphs) that can be assigned to a variable: ● Data Objects: used to store real or complex numerical values, logical values or characters. These objects are always vectors: there are no scalars in R. ● Language Objects: functions, expressions
  • 13. 13 Data structure types Vectors: one-dimensional arrays used to store collection data of the same mode ●Numeric Vectors (mode: numeric) ●Complex Vectors (mode: complex) ●Logical Vectors (model: logical) ●Character Vector or text strings (mode: character) Matrices: two-dimensional arrays to store collections of data of the same mode. They are accessed by two integer indices.
  • 14. 14 Data structure types Arrays: similar to matrices but they can be multi-dimensional (more than two dimensions) Factors: vectors of categorical variables designed to group the components of another vector with the same size Lists: ordered collection of objects, where the elements can be of different types Data Frames: generalization of matrices where different columns can store different mode data. Functions: objects created by the user and reused to make specific operations.
  • 16. 16 Numeric Vectors There are several ways to assign values to a variable: > a <- 1.7 > 1.7 -> a > a = 1.7 > assign("a", 1.7) To show the values: > a [1] 1.7 > print(a) [1] 1.7
  • 17. 17 Numeric Vectors To generate a vector with several numeric values: > a <- c(10, 11, 15, 19) The operations are always done over all the elements of the numeric array: > 1/a [1] 0.10000000 0.09090909 0.06666667 0.05263158 > b <- a-1 > b [1] 9 10 14 18 To generate a sequence: > 2:10 [1] 2 3 4 5 6 7 8 9 10
  • 18. 18 Logical Vectors a <- seq(1:10) > a [1] 1 2 3 4 5 6 7 8 9 10 > b <- (a>5) > b [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > a[b] [1] 6 7 8 9 10 > a[a>5] [1] 6 7 8 9 10
  • 19. 19 Character Vectors a <- "This is an example" > a [1] "This is an example" We can concatenate vectors after converting them into character vectors: > x <- 1.5 > y <- -2.7 > paste("Point is (",x,",",y,")", sep="") [1] "Point is (1.5,-2.7)"
  • 20. 20 Matrices A matrix is a bi-dimensional collection of data: > a <- matrix(1:12, nrow=3, ncol=4) > a [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > dim(a) [1] 3 4
  • 21. 21 Arrays They are similar to the matrices although they can have 2 o more dimensions. > z <- array(1:24, dim=c(2,3,4)) > z , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ... , , 4 [,1] [,2] [,3] [1,] 19 21 23 [2,] 20 22 24
  • 22. 22 Factors Factors are vectors that contain categorical information useful to group the values of other vectors of the same size. Let’s see an example: > bv <- c(0.92,0.97,0.87,0.91,0.92,1.04,0.91,0.94,0.96, + 0.90,0.96,0.86,0.85) # (B-V) colours from 13 galaxies
  • 23. 23 Lists Lists are ordered collections of objects, where the elements can be of a different type (a list can be a combination of matrices, vectors, other lists, etc.) They are created using the list() function: > gal <- list(name="NGC3379", morf="E", T.RC3=-5, colours=c(0.53,0.96)) > gal $name [1] "NGC3379" $morf [1] "E" $T.RC3 [1] -5 $colours [1] 0.53 0.96
  • 24. 24 Data Frames (Tables) A Data Frame is an special type of list very useful for the statistical work. There are some restrictions to guarantee that they can be used for this statistical purpose. Among other restrictions, a Data Frame must verify that: ●List components must be vectors (numeric, character or logical vectors), factors, numeric matrices or other data frames. ●Vectors, which are the variables in the data frame, must be of the same length.
  • 25. 25 Control statements ● Conditional execution : if statements ● Repetitive execution: for loops, repeat and while
  • 26. 26 if statements The syntax of if statement is: if (test_expression) { statement } The syntax of if...else statement is: if (test_expression) { statement1 } else { statement2 }
  • 27. 27 for loops for ( name in expr_1 ) expr_2 expr: expression Ex: for (i in 1:10) { if (!i %% 2){ next } print(i) } [1] 1 3 5 7 9
  • 28. 28 repeat repeat { statement } Ex: x <- 1 repeat { print(x) x = x+1 if (x == 6){ break } } [1] 1 2 3 4 5
  • 29. 29 while while(cond) expr cond: condition expr: expression Example: > x <- 1 > while(x < 5) {x <- x+1; print(x);} [1] 2 3 4
  • 30. 30 Operators We have the following types of operators in R programming: ● Arithmetic Operators ( + , - , * , / , %% [give the remainder] , %/% [result of division or quotient] , ^ [exponent] ) ● Relational Operators ( > , < , == , <= , >= , != ) ● Logical Operators ( & , | , ! , && , || ) ● Assignment Operators ( ← or = or < [Called Left Assignment ] , → or →> [Called Right Assignment] ) ●Miscellaneous Operators ( : [creates the series of numbers in sequence for a vector] , %in% [This operator is used to identify if an element belongs to a vector] , %*% [multiply a matrix with its transpose] )
  • 31. 31 read and write In R, we can read data from files stored outside the R environment. We can also write data into files which will be stored and accessed by the operating system. R can read and write into various file formats like csv, excel, xml etc. read.table() Ex: Mydata ← read.table(“c:/test/data.txt”) if you want to use back slash you should do this: Mydata ← read.table(“c:testdata.txt”) write.table(x, file=” ”) write.csv2(x, file=”*.csv”)
  • 32. 32 User Interfaces for R Rstudio Integrated development environment (IDE) for R Rattle Gnome cross platform GUI for Data Mining using RRed-R Open source visual programming interface for Rdeducer Intuitive, cross-platform graphical data analysis system RKWard Easy to use, transparent frontend JGR Universal and unified graphical user interface for R R Commander Basic-Statistics GUI for R terminal Linux terminal
  • 33. 33 Real world scenario : Mandelbrot set Short R code calculating Mandelbrot set through the first 20 iterations of equation z = z^2 + c plotted for different complex constants c. This example demonstrates: ● use of community-developed external libraries (called packages), in this case caTools package ● handling of complex numbers ● multidimensional arrays of numbers used as basic data type, see variables C, Z and X.
  • 34. 34 Real world scenario : Mandelbrot set install.packages("caTools") # install external package library(caTools) # external package providing write.gif function jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) dx <- 400 # define width dy <- 400 # define height C <- complex( real=rep(seq(-2.2, 1.0, length.out=dx), each=dy ), imag=rep(seq(-1.2, 1.2, length.out=dy), dx ) ) C <- matrix(C,dy,dx) # reshape as square matrix of complex numbers Z <- 0 # initialize Z to zero X <- array(0, c(dy,dx,20)) # initialize output 3D array for (k in 1:20) { # loop with 20 iterations Z <- Z^2+C # the central difference equation X[,,k] <- exp(-abs(Z)) # capture results } write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=900)
  • 35. 35 Real world scenario : Mandelbrot set