Introduction to R
Computational Economics Practice
Winter Term 2015/16
Stefan Feuerriegel
Today’s Lecture
Objectives
1 Being able to perform simple calculations in R
2 Understanding the concepts of variables
3 Handling vectors and matrices
Introduction to R 2
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R 3
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: General Information 4
Examples of Optimization Software
Excel Limited capabilities for optimization; good for data
preprocessing
Matlab Optimization toolbox, mainly aimed at engineering
GAMS Optimization only, but challenging user interface
CPLEX Optimization software package, but commercial
Introduction to R: General Information 5
What is R?
I Free software environment
aimed at statistical
computing
I Supports many operating
systems (Linux, Mac OS X,
Windows)
I Very frequently used in
psychology, bioinformatics,
statistics, econometrics,
machine learning and
optimization
Retrieving R
Download at [Link]
Introduction to R: General Information 6
R Studio as Editor
I Instead of typing commands
into the R Console, you can
generate commands by an
editor and then send them to
the R window
I . . . and later modify (correct)
them and send again
Retrieving R Studio (recommended)
Download at [Link]
Introduction to R: General Information 7
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Operations, Functions, Variables 8
First Example
→ Live Demonstration
3*(4+2)
## [1] 18
Introduction to R: Operations, Functions, Variables 9
Arithmetic Operations
1+2*3
## [1] 7
3/4+2 Operation Description Example Result
+ Plus 3+4 7
## [1] 2.75 - Minus 3-4 −1
* Times 3*4 12
2*pi-pi / Divide 3/4 0.75
^ Exponentiation 3^4 34 = 81
## [1] 3.141593
0/0
## [1] NaN
Introduction to R: Operations, Functions, Variables 10
Logic Operators
Comparison Operators
Operators <, <=, ==, !=, >=, > return boolean values TRUE or FALSE
3 < 4 4 == 4
## [1] TRUE ## [1] TRUE
3 > 4 3 != 4
## [1] FALSE ## [1] TRUE
3 <= 4
## [1] TRUE
Introduction to R: Operations, Functions, Variables 11
Brackets, Comments and Decimal Points
I Brackets can be used to prioritize evaluations
3*(4+2)
## [1] 18
I Important to use a point instead of a comma!
3.141
## [1] 3.141
I Comments via #
3+4 # will be ignored
## [1] 7
Introduction to R: Operations, Functions, Variables 12
Mathematical Functions
I Square root
sqrt(1+1)
## [1] 1.414214
I Logarithm to the base 10
log10(10*10*10)
## [1] 3
I Sinus function and rounding
sin(pi) # rarely exact: R uses limited number of digits
## [1] 1.224606e-16
round(sin(pi))
## [1] 0
Introduction to R: Operations, Functions, Variables 13
Mathematical Functions
Function Description Example Result
abs() Absolute Value abs(3-4) +1
round() Rounding round(3.14) √ ≈ 3
sqrt() Square Root sqrt(81) 81 = 9
sin() Sine sin(0) sin 0 = 0
cos() Cosine cos(0) cos 0 = 1
tan() Tangent tan(0) tan 0 = 0
log() Natural Logarithm log(e) ln e = 1
log10() Common Logarithm log10(100) log10 100 = 2
Introduction to R: Operations, Functions, Variables 14
Exercise: Mathematical Functions
Question
I What is the value of abs(3-4*5)?
I Visit [Link] with code 1523
Introduction to R: Operations, Functions, Variables 15
Variables
x <- 2
I Variables store values during a session
x I Value on right is assigned to variable
preceding "<-"
## [1] 2
I No default output after assignment
x+3 I Recommended names consist of letters
A–Z plus "_" and "."
## [1] 5
I Must not contain minus!
x I Should be different from function
names, e. g. sin
## [1] 2 I Good: x, fit, ratio, etc.
I Warning: naming is case-sensitive
x <- x+4 I i. e. x and X are different
x
## [1] 6
Introduction to R: Operations, Functions, Variables 16
Exercise: Variables
Question
I What is the value of z?
I Visit [Link] with code 1523
x <- 2
x <- x+1
y <- 4
z <- x+y
x <- x+1
z <- z+x
Introduction to R: Operations, Functions, Variables 17
Strings
I Sequence of characters are named strings
I Surrounded by double quotes (")
I Necessary for e. g. naming column names
"Text"
## [1] "Text"
"3.14"
## [1] "3.14"
"3.14"+1 # mixing strings and numbers does not work
## Error in "3.14" + 1: nicht-numerisches Argument für
binären Operator
Introduction to R: Operations, Functions, Variables 18
Help Pages
Accessing help pages for each function via help(func)
help(sin)
Introduction to R: Operations, Functions, Variables 19
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Vectors 20
Creating and Accessing Vectors
I Create vector filled with I Selecting a range of
zeros via numeric(n) elements
numeric(4) x[c(2,3)]
## [1] 0 0 0 0 ## [1] 0 6
I Vector elements are I Selecting everything but a
concatenated via c(...) subset of elements
x <- c(4, 0, 6) x[-1]
x
## [1] 0 6
## [1] 4 0 6
x[-c(2,3)]
I Accessing individual ## [1] 4
elements via squared
brackets [] I Dimension via length()
x[1] # first component length(x)
## [1] 4 ## [1] 3
Introduction to R: Vectors 21
Updating Vectors
x <- c(4, 0, 6)
I Replacing values
x[1] <- 2 # replace first component
x
## [1] 2 0 6
I Appending elements
y <- c(x, 8) # append an element
y
## [1] 2 0 6 8
Introduction to R: Vectors 22
Vectors: Concatenation
x <- c(4, 0, 6)
y <- c(8, 9)
I Combining several vectors is named concatenation
z <- c(x, y) # concatenating two vectors
z
## [1] 4 0 6 8 9
I Replicating elements by rep(val, count) to form vectors
rep(1, 5) # 5-fold replication of the value 1
## [1] 1 1 1 1 1
rep(c(1, 2), 3) # repeat vector 3 times
## [1] 1 2 1 2 1 2
Introduction to R: Vectors 23
Vector Functions
x <- c(1, 2, 3, 0, 10)
I Average value
mean(x)
## [1] 3.2
I Variance
var(x)
## [1] 15.7
I Sum of all elements
sum(x)
## [1] 16
Introduction to R: Vectors 24
Exercise: Vectors
Question
1
I How to compute a standard deviation of x = 4 ?
9
I sqr(var(x))
I sqrt(var(x))
I sd(x)
I Visit [Link] with code 1523
Introduction to R: Vectors 25
Vector Operations
x <- c(1, 2)
y <- c(5, 6)
I Scaling
10*x
## [1] 10 20
I Addition
x+y
## [1] 6 8
10+x
## [1] 11 12
I Be careful with functions such as sin() on vectors!
Introduction to R: Vectors 26
Generating Sequences
I Integer sequences
1:4
## [1] 1 2 3 4
4:1
## [1] 4 3 2 1
I Arbitrary sequences
(1:10)/10
## [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
seq(4, 5, 0.1) # notation: start, end, step size
## [1] 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0
Introduction to R: Vectors 27
Exercise: Vectors
Question
100
I How to compute ∑ i?
i =1
I sum(1:100)
I sum(1,100)
I sum(1-100)
I Visit [Link] with code 1523
Introduction to R: Vectors 28
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Matrices 29
Matrices from Combining Vectors
I Generating matrices by combining vectors with cbind(...)
height <- c(163, 186, 172)
shoe_size <- c(39, 44, 41)
m <- [Link](cbind(height, shoe_size))
. . . but exhausting!
I [Link](...) necessary to store data of different types
(numeric, strings, etc.)
Introduction to R: Matrices 30
Files formatted as Comma Separated Values
I Support of naive Excel format is unsatisfactory
I Recommended: Export as Comma Separated Values (CSV)
I In Excel via Save As → file type is CSV (Comma separated)
I Then: right mouse click → Open with → Text Editor → Check if there
are commas
Example File: [Link]
name,height,shoesize,age
Julia,163,39,24
Robin,186,44,26
Kevin,172,41,21
Max,184,43,22
Jerry,193,45,31
Introduction to R: Matrices 31
Matrices from Text Files
[Link](filename, ...) imports data frame from text file
I header=TRUE specifies whether columns have names
I sep="," specifies column delimiter
I [Link](...) guarantees output as data frame
d <- [Link]([Link]("[Link]",
header=TRUE, sep=","))
d
## name height shoesize age
## 1 Julia 163 39 24
## 2 Robin 186 44 26
## 3 Kevin 172 41 21
## 4 Max 184 43 22
## 5 Jerry 193 45 31
I Alternatively, choose path to file via [Link]() manually
d <- [Link]([Link]([Link](),
header=TRUE, sep=","))
Introduction to R: Matrices 32
Output: Matrices
I Show first 6 rows only (useful for large files)
head(d)
## name height shoesize age
## 1 Julia 163 39 24
## 2 Robin 186 44 26
## 3 Kevin 172 41 21
## 4 Max 184 43 22
## 5 Jerry 193 45 31
I Show column names
str(d)
## '[Link]': 5 obs. of 4 variables:
## $ name : Factor w/ 5 levels "Jerry","Julia",..: 2 5 3 4 1
## $ height : int 163 186 172 184 193
## $ shoesize: int 39 44 41 43 45
## $ age : int 24 26 21 22 31
Introduction to R: Matrices 33
Accessing Matrices
I Dimension (#rows, #columns) or number of rows/columns
dim(d) nrow(d)
## [1] 5 4 ## [1] 5
ncol(d)
## [1] 4
I Access columns by name
d$height
## [1] 163 186 172 184 193
d[["height"]]
## [1] 163 186 172 184 193
I Accessing an individual element (notation: #row, #column)
d[1,2]
## [1] 163
Introduction to R: Matrices 34
Selecting Elements
I Using single condition to select a subset of rows
d[d$age > 25, ]
## name height shoesize age
## 2 Robin 186 44 26
## 5 Jerry 193 45 31
d[d$age == 32, ]
## [1] name height shoesize age
## <0 rows> (or 0-length [Link])
I Connecting several conditions (& is and, | is or)
d[d$age < 25 & d$height <= 163, ]
## name height shoesize age
## 1 Julia 163 39 24
Introduction to R: Matrices 35
Exercise: Selecting Elements
Question
I How to select all elements with age 26 or shoesize 45?
I d[d$age = 26 | d$shoesize = 45, ]
I d[d$age == 26 | d$shoesize == 45, ]
I d[d$age == 26 | d$shoesize == 45]
I d[d$age == 26 & d$shoesize == 45, ]
I Visit [Link] with code 1523
Introduction to R: Matrices 36
Adding Columns and Column Names
I Adding columns
d[["heightInInch"]] <- d$height/2.51
d$heightInInch
## [1] 64.94024 74.10359 68.52590 73.30677 76.89243
I Getting column names via colnames()
colnames(d)
## [1] "name" "height" "shoesize" "age"
## [5] "heightInInch"
I Updating column names
colnames(d) <- c("name", "waist", "weight", "shoes",
"books")
colnames(d)
## [1] "name" "waist" "weight" "shoes" "books"
Introduction to R: Matrices 37
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Extensibility 38
Extending R: Packages
I Most routines (from e. g. time series, statistical tests, plotting) are in
so-called packages
I Packages must be downloaded & installed before usage
I When accessing routines, must be loaded via library(package)
I Installing packages by clicking:
In R Console In R Studio
I Menu Packages I Menu Tools
I Install package(s) . . . I Install packages
I Choose arbitrary server I Enter package name in
I Choose package middle input box
I Press Install
Introduction to R: Extensibility 39
Exercise
Question
I You are doing an analysis in R and need to use the summary()
function but you are not exactly sure how it works. Which of the
following commands should you run?
I help(summary)
I ?summary
I man(summary)
I ?summary()
I Visit [Link] with code 1523
Introduction to R: Extensibility 40
Outline
1 General Information
2 Operations, Functions, Variables
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Wrap-Up 41
Tutorials on Using R
I Search Internet → many tutorials available online
I R Manual is the official introductory document
→ [Link]
I Helpful examples and demonstrations
→ [Link]
I Help pages in R describe parameters in detail, contain examples, but
aim at advanced audience
Introduction to R: Wrap-Up 42
Recommended Books
I German books
I R-Einführung: Einführung durch angewandte Statistik
(Pearson, 2011, by Hatzinger, Hornik & Nagel)
[Link]
I English books (highly recommended)
I R in Action: Data Analysis and Graphics with R
(Manning, 2011, by Kabacoff, same as [Link])
I R Cookbook
(O’Reilly, 2011, by Teetor)
Introduction to R: Wrap-Up 43
Summary: Commands
+, -, etc. Algebraic operators
&, |, <, <=, etc. Logic operators
help(func) Help pages
mean(), var() Functions on vectors
sd() Standard deviation
seq() Generate sequences
d$column Accessing columns of a matrix
[Link]() Reading text files
Introduction to R: Wrap-Up 44
Outlook
Additional Material
I Short summary of today’s lecture → Seminar Paper
I Further exercises as homework
Future Exercises
R will be used to solve sample optimization problems
Introduction to R: Wrap-Up 45