SlideShare a Scribd company logo
THE R LANGUAGE FOR
STATISTICAL COMPUTING
Lecture 2
Dr. Syed
Muhammad Ali
Tirmizi
Chapter No. 2
1
2
INTRODUCTION
 R language has its roots at AT&T Bell
Laboratories during the 1970s and 1980s in the
S language project (Becker, Chambers, and
Wilks, 1988).
 People think that the S language would not have
been designed in the way it was if it had been
designed by computer scientists (Morandat, Hill,
Osvald, and Vitek, 2012).
3
INTRODUCTION
 R is an open source variant of S developed at the
University of Auckland by Ross Ihaka and Robert
Gentleman, first appearing in 1993 (Ihaka, 1998).
 Clearly the recent popularity of R, fueled by its
open source availability and the need for statistical
and analytical computing tools, shows that the
benefits of R far outweigh the negatives.
 Overall, R is based upon the vector as a first class
item in the language.
4
INTRODUCTION
 R shares this attribute with LISP, Scheme,
Python, and Matlab.
 This and the prevalence of over 4,000 publicly
available packages are two of the many
strengths of R.
 In this course, we will focus on R packages that
revolve around financial analytics.
5
GETTING STARTED WITH R
 One of the great things about R is how easy it is to install.
 In your browser, head to the web site for the Comprehensive R
Archive Network (CRAN), http://cran.r-project.org
 This website will help you to download and install R software for
windows.
 Just as a basic test, we can create a vector of prices and plot it with
this block of code:
x = c(1.3,1.2,1.3,NA,1.4,1.5)
plot(x,ylab="EUR prices")
is.na(x)
[1] FALSE FALSE FALSE TRUE FALSE FALSE
6
GETTING STARTED WITH R
 The c() operator creates a vector of elements.
 This is the basic vector operator in R.
 Note the “not available” (NA) element
appearing as the fourth item of the vector.
 R’s ability to handle NAs, infinite values (Inf),
and not a number (NaN) is one of its many
strengths.
7
GETTING STARTED WITH R
 A later development is the R Studio GUI available
from the web site www.rstudio.com.
 R Studio is a commercially developed GUI allowing
management of plot windows, variable contents,
and better debugging than the basic R interpreter.
 R Studio is a second-generation R user interface
with integrated code, execution, variable inspection,
and plotting windows, and expression completion.
8
9
GETTING STARTED WITH R
 Any time a library statement is encountered, R
will check that the package is available.
 If not, it must be downloaded.
 As an example, to download the ggplot2
package, use the following command:
update.packages()
install.packages("ggplot2",dependencies=TRUE)
library(ggplot2)
10
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 For many use cases, R provides a computational
statistics platform.
 Mathematical functions are readily available. The
basic log() function provides a natural logarithm.
Of course, executing log() on a vector, x, results
in a vector of natural logarithms, y.
11
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 The computation of log() on NA is NA as expected.
> #Filter prices:
> x[x > 1.3]
[1] NA 1.4 1.5
> #Keeps the NA intact:
> y <- diff(log(x))
> round(y,3)
[1] -0.080 0.080 NA NA 0.069
12
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 In R, not only vectors but also functions are first-
class objects. It shares this attribute with the
functional languages LISP and Scheme.
 Assigning a function to a variable is the usual
way to define it. If g is assigned to the function
definition, then g(4) will evaluate it and g,
without parentheses, will return its definition.
13
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES

14
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 R has four assignment operators.
 The most basic operator is “<-”. This is the one
we use in the first assignment in the code block
below.
 R’s functional nature is so strong that even this
can be replaced by the function call assign(“x”,1).
15
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 Over time, because people were used to other
languages that use “=” instead, and even though
“=” was used to assign parameter values in
function calls (g(x, y = 7), for example), it was
also made available for assignment in R.
 So using “<-” or “=” is now really a matter of
preference.
16
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
> x <- 1
> assign("x",2)
> x
[1] 2
> x = 3
> x
[1] 3
> f <-function(x)
+ {
+ x = 4
+ x
+ }
> f(x)
[1] 4
> x
[1] 3
RUN THESE
CODES
17
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 R’s fourth assignment operator, with two “<”s, is
known as the “super-assignment” operator.
 Executing it will look outside the current frame
for x, which is global to the function f , and
assign to that x.
 If there is no x in the global environment, it will
create one and assign the value to it.
18
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
> #The fourth type is "<<-"
> x = 3
> x
[1] 3
> f <-function(x)
+ {
+ x <<- 4
+ x
+ }
> f(x)
[1] 4
> x
[1] 4
> typeof(f)
[1] "closure"
> typeof(x)
[1] "double"
RUN THESE
CODES
19
LANGUAGE FEATURES: FUNCTIONS,
ASSIGNMENT, ARGUMENTS, AND TYPES
 R is dynamically typed so that variables do not have types.
 Instead, values have types.
 So we can see that the type of a variable is determined by
the type of its current value.
 The function typeof() can be used to return the type of the
value assigned to a variable.
 We can see its use in the output block above: typeof(f) is a
“closure” for a function while typeof(x) is a “double.”
20
LANGUAGE FEATURES:
BINDING AND ARRAYS
 Binding scalars or vectors together is one way to return
aggregate results from functions.
 For binding, cbind() binds items into two columns and
rbind() binds items into two rows.
 If the two items are scalars then the two operations are
equivalent. rep() is a very common function to create a
vector of repeated items.
 For example, rep(4, 5) == c(4, 4, 4, 4, 4) is TRUE and
states to repeat 4 five times.
21
LANGUAGE FEATURES:
BINDING AND ARRAYS
> #Create two column matrix:
> A = cbind(rep(x,length(y)),y)
> A
y
[1,] 4 -0.08004271
[2,] 4 0.08004271
[3,] 4 NA
[4,] 4 NA
[5,] 4 0.06899287
> B = rbind(rep(x,length(y)),y)
> B
[,1] [,2] [,3] [,4] [,5]
4.00000000 4.00000000 4 4 4.00000000
y -0.08004271 0.08004271 NA NA 0.06899287
> t(A) == B
[,1] [,2] [,3] [,4] [,5]
TRUE TRUE TRUE TRUE TRUE
y TRUE TRUE NA NA TRUE
> sum(t(A) == B)
[1] NA
RUN THESE
CODES
22
ERROR HANDLING
 Error handling is an important part of data science, in
order to keep erroneous data from making its way into the
variables and to keep the dataset as clean as possible.
 When calling certain packages, it is common to have
errors returned. R has a tryCatch() feature which is
implemented as a function.
 Example, we use division by zero as the type of error
handled.
23
ERROR HANDLING
#Exception handling:
fh <- 0
tryCatch({
#main block
fh <<- file("file1.txt", open="r")
}, warning = function(w) {
#warning-handler-code
print(w)
fh <<- NA
}, error = function(e) {
#error-handler-code
print(e)
fh <<- NA
}, finally = {
#cleanup-code
})
if(!is.na(fh)) readLines(fh)
RUN THESE
CODES
24
NUMERIC, STATISTICAL,
AND CHARACTER FUNCTIONS
 To set the mode of calculations, options(digits = n) sets
the number of digits to round to in calculations.
#Setting precision:
options(digits=10)
pi = 3.1415926535897932384626
pi
 [1] 3.141592654
25
NUMERIC, STATISTICAL,
AND CHARACTER FUNCTIONS
 Distributions of random variates are available readily
within the language with runif() for the Uniform
Distribution, rnorm() for the Normal Distribution, and
rbinom() for the Binomial Distribution, to name just
three.
 Histograms and density plots are also native to the
language with hist() and density().
26
NUMERIC, STATISTICAL,
AND CHARACTER FUNCTIONS
plot(density(rbinom(50,50,1/2))
options(digits=6)
set.seed(99)
sample(10, replace = TRUE)
[1] 6 2 7 10 6 10 7 3 4 2
27
DATA FRAMES AND INPUT–OUTPUT
 Data frames are one of R’s more unusual and handy
features.
 A data frame is a sequence of rows where the columns are
heterogeneously typed. A common way of loading data
frames is from the Excel .csv files.
#Input-ouput:
write.csv(d,file="d.txt",row.names=FALSE)
e <- read.csv("d.txt",header=TRUE)
28
LISTS
 Lists are ordered aggregates like vectors, which are
constructed by c(. . .).
 However, lists differ from the basic vectors, in that they
are recursively formed, using list(. . .).
#Lists:
c(1,c(1,2),3,"A",c(4,5))
[1] "1" "1" "2" "3" "A" "4" "5"
list(1,c(1,2),3,"A",list(4,5))
THANK YOU
29

More Related Content

DOCX
Report on c and c++
PDF
PDF
Introduction to functional programming
PDF
answer-model-qp-15-pcd13pcd
PDF
VTU PCD Model Question Paper - Programming in C
PPS
basics of C and c++ by eteaching
PPTX
Back patching
PDF
Lecture 3 RE NFA DFA
Report on c and c++
Introduction to functional programming
answer-model-qp-15-pcd13pcd
VTU PCD Model Question Paper - Programming in C
basics of C and c++ by eteaching
Back patching
Lecture 3 RE NFA DFA

What's hot (20)

PDF
Reduce course notes class xii
PPTX
C programming language tutorial
PDF
Compiler unit 5
PPTX
C Language (All Concept)
PPTX
Three address code In Compiler Design
PDF
C notes.pdf
PPTX
Intermediate code generation1
PPT
C program
PPT
Interm codegen
PPTX
Intermediate code- generation
PPT
Introduction to c programming
ODP
OpenGurukul : Language : C Programming
PDF
C Programming
PPT
C language
PPT
Chapter 6 intermediate code generation
PDF
Intro to Functional Reactive Programming In Scala
PDF
Actors and functional_reactive_programming
PPTX
Introduction Of C++
PDF
C Programming Tutorial - www.infomtec.com
PDF
Alp 05
Reduce course notes class xii
C programming language tutorial
Compiler unit 5
C Language (All Concept)
Three address code In Compiler Design
C notes.pdf
Intermediate code generation1
C program
Interm codegen
Intermediate code- generation
Introduction to c programming
OpenGurukul : Language : C Programming
C Programming
C language
Chapter 6 intermediate code generation
Intro to Functional Reactive Programming In Scala
Actors and functional_reactive_programming
Introduction Of C++
C Programming Tutorial - www.infomtec.com
Alp 05
Ad

Similar to Special topics in finance lecture 2 (20)

PPTX
R Programming Language
PPTX
1_Introduction.pptx
PPTX
R basics for MBA Students[1].pptx
PPT
Inroduction to r
PPTX
Introduction to R for beginners
PDF
Unit 1 r studio programming required.pdf
PDF
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
PDF
FULL R PROGRAMMING METERIAL_2.pdf
PPT
Best corporate-r-programming-training-in-mumbai
PDF
PDF
quantile regression in R Roger Koenker.pdf
PPT
Lecture1_R Programming Introduction1.ppt
PPT
Brief introduction to R Lecturenotes1_R .ppt
PPT
R_Language_study_forstudents_R_Material.ppt
PPT
Lecture1_R.ppt
PPT
Modeling in R Programming Language for Beginers.ppt
PPT
Lecture1_R.ppt
PPT
Lecture1 r
PDF
Dot Call interface
PPT
Lecture_R.ppt
R Programming Language
1_Introduction.pptx
R basics for MBA Students[1].pptx
Inroduction to r
Introduction to R for beginners
Unit 1 r studio programming required.pdf
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
FULL R PROGRAMMING METERIAL_2.pdf
Best corporate-r-programming-training-in-mumbai
quantile regression in R Roger Koenker.pdf
Lecture1_R Programming Introduction1.ppt
Brief introduction to R Lecturenotes1_R .ppt
R_Language_study_forstudents_R_Material.ppt
Lecture1_R.ppt
Modeling in R Programming Language for Beginers.ppt
Lecture1_R.ppt
Lecture1 r
Dot Call interface
Lecture_R.ppt
Ad

More from Dr. Muhammad Ali Tirmizi., Ph.D. (19)

PPTX
Financial Risk Mgt - Lec 14 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 13 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 12 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 11 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 10 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 9 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 8 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 7 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 5 & 6 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 4 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 3 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 2 by Dr. Syed Muhammad Ali Tirmizi
PPTX
Financial Risk Mgt - Lec 1 by dr. syed muhammad ali tirmizi
PPTX
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 14
PPTX
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 13
PPTX
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 5
PPTX
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 4
PPTX
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 3
PPTX
Special topics in finance lec 1
Financial Risk Mgt - Lec 14 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 13 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 12 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 11 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 10 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 9 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 8 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 7 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 5 & 6 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 4 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 3 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 2 by Dr. Syed Muhammad Ali Tirmizi
Financial Risk Mgt - Lec 1 by dr. syed muhammad ali tirmizi
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 14
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 13
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 5
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 4
Dr. Syed Muhammad Ali Tirmizi - Special topics in finance lec 3
Special topics in finance lec 1

Recently uploaded (20)

PPTX
CYBER SECURITY the Next Warefare Tactics
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PDF
annual-report-2024-2025 original latest.
PPTX
modul_python (1).pptx for professional and student
PDF
Transcultural that can help you someday.
PDF
Global Data and Analytics Market Outlook Report
PDF
Introduction to Data Science and Data Analysis
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PDF
Introduction to the R Programming Language
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
IMPACT OF LANDSLIDE.....................
PDF
[EN] Industrial Machine Downtime Prediction
PDF
Microsoft Core Cloud Services powerpoint
PPTX
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
PDF
Navigating the Thai Supplements Landscape.pdf
CYBER SECURITY the Next Warefare Tactics
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
annual-report-2024-2025 original latest.
modul_python (1).pptx for professional and student
Transcultural that can help you someday.
Global Data and Analytics Market Outlook Report
Introduction to Data Science and Data Analysis
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
Introduction to the R Programming Language
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
IMPACT OF LANDSLIDE.....................
[EN] Industrial Machine Downtime Prediction
Microsoft Core Cloud Services powerpoint
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
Navigating the Thai Supplements Landscape.pdf

Special topics in finance lecture 2

  • 1. THE R LANGUAGE FOR STATISTICAL COMPUTING Lecture 2 Dr. Syed Muhammad Ali Tirmizi Chapter No. 2 1
  • 2. 2 INTRODUCTION  R language has its roots at AT&T Bell Laboratories during the 1970s and 1980s in the S language project (Becker, Chambers, and Wilks, 1988).  People think that the S language would not have been designed in the way it was if it had been designed by computer scientists (Morandat, Hill, Osvald, and Vitek, 2012).
  • 3. 3 INTRODUCTION  R is an open source variant of S developed at the University of Auckland by Ross Ihaka and Robert Gentleman, first appearing in 1993 (Ihaka, 1998).  Clearly the recent popularity of R, fueled by its open source availability and the need for statistical and analytical computing tools, shows that the benefits of R far outweigh the negatives.  Overall, R is based upon the vector as a first class item in the language.
  • 4. 4 INTRODUCTION  R shares this attribute with LISP, Scheme, Python, and Matlab.  This and the prevalence of over 4,000 publicly available packages are two of the many strengths of R.  In this course, we will focus on R packages that revolve around financial analytics.
  • 5. 5 GETTING STARTED WITH R  One of the great things about R is how easy it is to install.  In your browser, head to the web site for the Comprehensive R Archive Network (CRAN), http://cran.r-project.org  This website will help you to download and install R software for windows.  Just as a basic test, we can create a vector of prices and plot it with this block of code: x = c(1.3,1.2,1.3,NA,1.4,1.5) plot(x,ylab="EUR prices") is.na(x) [1] FALSE FALSE FALSE TRUE FALSE FALSE
  • 6. 6 GETTING STARTED WITH R  The c() operator creates a vector of elements.  This is the basic vector operator in R.  Note the “not available” (NA) element appearing as the fourth item of the vector.  R’s ability to handle NAs, infinite values (Inf), and not a number (NaN) is one of its many strengths.
  • 7. 7 GETTING STARTED WITH R  A later development is the R Studio GUI available from the web site www.rstudio.com.  R Studio is a commercially developed GUI allowing management of plot windows, variable contents, and better debugging than the basic R interpreter.  R Studio is a second-generation R user interface with integrated code, execution, variable inspection, and plotting windows, and expression completion.
  • 8. 8
  • 9. 9 GETTING STARTED WITH R  Any time a library statement is encountered, R will check that the package is available.  If not, it must be downloaded.  As an example, to download the ggplot2 package, use the following command: update.packages() install.packages("ggplot2",dependencies=TRUE) library(ggplot2)
  • 10. 10 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  For many use cases, R provides a computational statistics platform.  Mathematical functions are readily available. The basic log() function provides a natural logarithm. Of course, executing log() on a vector, x, results in a vector of natural logarithms, y.
  • 11. 11 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  The computation of log() on NA is NA as expected. > #Filter prices: > x[x > 1.3] [1] NA 1.4 1.5 > #Keeps the NA intact: > y <- diff(log(x)) > round(y,3) [1] -0.080 0.080 NA NA 0.069
  • 12. 12 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  In R, not only vectors but also functions are first- class objects. It shares this attribute with the functional languages LISP and Scheme.  Assigning a function to a variable is the usual way to define it. If g is assigned to the function definition, then g(4) will evaluate it and g, without parentheses, will return its definition.
  • 14. 14 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  R has four assignment operators.  The most basic operator is “<-”. This is the one we use in the first assignment in the code block below.  R’s functional nature is so strong that even this can be replaced by the function call assign(“x”,1).
  • 15. 15 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  Over time, because people were used to other languages that use “=” instead, and even though “=” was used to assign parameter values in function calls (g(x, y = 7), for example), it was also made available for assignment in R.  So using “<-” or “=” is now really a matter of preference.
  • 16. 16 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES > x <- 1 > assign("x",2) > x [1] 2 > x = 3 > x [1] 3 > f <-function(x) + { + x = 4 + x + } > f(x) [1] 4 > x [1] 3 RUN THESE CODES
  • 17. 17 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  R’s fourth assignment operator, with two “<”s, is known as the “super-assignment” operator.  Executing it will look outside the current frame for x, which is global to the function f , and assign to that x.  If there is no x in the global environment, it will create one and assign the value to it.
  • 18. 18 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES > #The fourth type is "<<-" > x = 3 > x [1] 3 > f <-function(x) + { + x <<- 4 + x + } > f(x) [1] 4 > x [1] 4 > typeof(f) [1] "closure" > typeof(x) [1] "double" RUN THESE CODES
  • 19. 19 LANGUAGE FEATURES: FUNCTIONS, ASSIGNMENT, ARGUMENTS, AND TYPES  R is dynamically typed so that variables do not have types.  Instead, values have types.  So we can see that the type of a variable is determined by the type of its current value.  The function typeof() can be used to return the type of the value assigned to a variable.  We can see its use in the output block above: typeof(f) is a “closure” for a function while typeof(x) is a “double.”
  • 20. 20 LANGUAGE FEATURES: BINDING AND ARRAYS  Binding scalars or vectors together is one way to return aggregate results from functions.  For binding, cbind() binds items into two columns and rbind() binds items into two rows.  If the two items are scalars then the two operations are equivalent. rep() is a very common function to create a vector of repeated items.  For example, rep(4, 5) == c(4, 4, 4, 4, 4) is TRUE and states to repeat 4 five times.
  • 21. 21 LANGUAGE FEATURES: BINDING AND ARRAYS > #Create two column matrix: > A = cbind(rep(x,length(y)),y) > A y [1,] 4 -0.08004271 [2,] 4 0.08004271 [3,] 4 NA [4,] 4 NA [5,] 4 0.06899287 > B = rbind(rep(x,length(y)),y) > B [,1] [,2] [,3] [,4] [,5] 4.00000000 4.00000000 4 4 4.00000000 y -0.08004271 0.08004271 NA NA 0.06899287 > t(A) == B [,1] [,2] [,3] [,4] [,5] TRUE TRUE TRUE TRUE TRUE y TRUE TRUE NA NA TRUE > sum(t(A) == B) [1] NA RUN THESE CODES
  • 22. 22 ERROR HANDLING  Error handling is an important part of data science, in order to keep erroneous data from making its way into the variables and to keep the dataset as clean as possible.  When calling certain packages, it is common to have errors returned. R has a tryCatch() feature which is implemented as a function.  Example, we use division by zero as the type of error handled.
  • 23. 23 ERROR HANDLING #Exception handling: fh <- 0 tryCatch({ #main block fh <<- file("file1.txt", open="r") }, warning = function(w) { #warning-handler-code print(w) fh <<- NA }, error = function(e) { #error-handler-code print(e) fh <<- NA }, finally = { #cleanup-code }) if(!is.na(fh)) readLines(fh) RUN THESE CODES
  • 24. 24 NUMERIC, STATISTICAL, AND CHARACTER FUNCTIONS  To set the mode of calculations, options(digits = n) sets the number of digits to round to in calculations. #Setting precision: options(digits=10) pi = 3.1415926535897932384626 pi  [1] 3.141592654
  • 25. 25 NUMERIC, STATISTICAL, AND CHARACTER FUNCTIONS  Distributions of random variates are available readily within the language with runif() for the Uniform Distribution, rnorm() for the Normal Distribution, and rbinom() for the Binomial Distribution, to name just three.  Histograms and density plots are also native to the language with hist() and density().
  • 26. 26 NUMERIC, STATISTICAL, AND CHARACTER FUNCTIONS plot(density(rbinom(50,50,1/2)) options(digits=6) set.seed(99) sample(10, replace = TRUE) [1] 6 2 7 10 6 10 7 3 4 2
  • 27. 27 DATA FRAMES AND INPUT–OUTPUT  Data frames are one of R’s more unusual and handy features.  A data frame is a sequence of rows where the columns are heterogeneously typed. A common way of loading data frames is from the Excel .csv files. #Input-ouput: write.csv(d,file="d.txt",row.names=FALSE) e <- read.csv("d.txt",header=TRUE)
  • 28. 28 LISTS  Lists are ordered aggregates like vectors, which are constructed by c(. . .).  However, lists differ from the basic vectors, in that they are recursively formed, using list(. . .). #Lists: c(1,c(1,2),3,"A",c(4,5)) [1] "1" "1" "2" "3" "A" "4" "5" list(1,c(1,2),3,"A",list(4,5))