Introduction To R
Introduction To R
Objectives
Introduction to R 2
Outline
1 General Information
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R 3
Outline
1 General Information
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Retrieving R
Download at http://www.r-project.org
1 General Information
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
→ Live Demonstration
3*(4+2)
## [1] 18
1+2*3
## [1] 7
+ 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
3 < 4 4 == 4
3 > 4 3 != 4
3 <= 4
## [1] TRUE
Question
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
Question
x <- 2
x <- x+1
y <- 4
z <- x+y
x <- x+1
z <- z+x
"Text"
## [1] "Text"
"3.14"
## [1] "3.14"
help(sin)
1 General Information
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Vectors 20
Creating and Accessing Vectors
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)
Introduction to R: Vectors 23
Vector Functions
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)
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
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 http://pingo.upb.de with code 1523
Introduction to R: Vectors 28
Outline
1 General Information
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Matrices 29
Matrices from Combining Vectors
. . . but exhausting!
I as.data.frame(...) necessary to store data of different types
(numeric, strings, etc.)
Introduction to R: Matrices 30
Files formatted as Comma Separated Values
Introduction to R: Matrices 31
Matrices from Text Files
read.csv(filename, ...) imports data frame from text file
I header=TRUE specifies whether columns have names
I sep="," specifies column delimiter
I as.data.frame(...) guarantees output as data frame
d <- as.data.frame(read.csv("persons.csv",
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 file.choose() manually
d <- as.data.frame(read.csv(file.choose(),
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)
## 'data.frame': 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
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 row.names)
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, ]
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
3 Vectors
4 Matrices
5 Extensibility
6 Wrap-Up
Introduction to R: Extensibility 38
Extending R: Packages
In R Console In R Studio
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()
Introduction to R: Extensibility 40
Outline
1 General Information
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
→ http://cran.r-project.org/doc/manuals/R-intro.pdf
I Helpful examples and demonstrations
→ http://www.statmethods.net
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)
http://lib.myilibrary.com/Open.aspx?id=404906
I English books (highly recommended)
I R in Action: Data Analysis and Graphics with R
(Manning, 2011, by Kabacoff, same as statmethods.net)
I R Cookbook
(O’Reilly, 2011, by Teetor)
Introduction to R: Wrap-Up 43
Summary: Commands
Introduction to R: Wrap-Up 44
Outlook
Additional Material
Future Exercises
R will be used to solve sample optimization problems
Introduction to R: Wrap-Up 45