Introduction to R for Data Science
Introduction to R for Data Science
PROGRAMMING LANGUAGE R
LECTURE: UNSUPERVISED LEARNING AND EVOLUTIONARY COMPUTATION
USING R
Jakob Bossek
MALEO Group, Department of Computer Science, Paderborn University, Germany
Forest reaching the ocean (for the first time ,); Forest Gump, ©Paramount pictures 1994.
Forest reaching the ocean (for the first time ,); Forest Gump, ©Paramount pictures 1994.
“That day, for no particular reason, I decided to go for a little run. So I ran to the end of the road. And
when I got there, I thought maybe I’d run to the end of town. And when I got there, I thought maybe I’d
just run across Greenbow County. And I figured, since I run this far, maybe I’d just run across the great
state of Alabama. And that’s what I did. I ran clear across Alabama. For no particular reason I just kept
on going. I ran clear to the ocean. And when I got there, I figured, since I’d gone this far, I might as well
turn around, just keep on going. When I got to another ocean, I figured, since I’d gone this far, I might
as well just turn back, keep right on going.” - Forrest Gump
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Preface
▶ My original goal: at most 30 slides to teach the very essentials
▶ However, presentation slides evolved to a deck of > 100 slides
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Preface
▶ My original goal: at most 30 slides to teach the very essentials
▶ However, presentation slides evolved to a deck of > 100 slides
▶ Consequence: we will skip a lot of stuff ,
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Preface
▶ My original goal: at most 30 slides to teach the very essentials
▶ However, presentation slides evolved to a deck of > 100 slides
▶ Consequence: we will skip a lot of stuff ,
▶ Benefit for you: quite a lot important aspects are covered. Take it as a reference
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Preface
▶ My original goal: at most 30 slides to teach the very essentials
▶ However, presentation slides evolved to a deck of > 100 slides
▶ Consequence: we will skip a lot of stuff ,
▶ Benefit for you: quite a lot important aspects are covered. Take it as a reference
▶ Many exercises without solutions (discuss with fellow students).1
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Preface
▶ My original goal: at most 30 slides to teach the very essentials
▶ However, presentation slides evolved to a deck of > 100 slides
▶ Consequence: we will skip a lot of stuff ,
▶ Benefit for you: quite a lot important aspects are covered. Take it as a reference
▶ Many exercises without solutions (discuss with fellow students).1
▶ Enjoy! ,
1
2nd exercise sheet will also contain many R exercises.
J. Bossek: A Gentle Introduction into R 3
Introduction
▶ R is a statistical programming language2 / environment3
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
4
Though R does a bad job at providing nice supporting tools. However, the community does via many
useful packages.
J. Bossek: A Gentle Introduction into R 4
Introduction
▶ R is a statistical programming language2 / environment3
▶ Includes a plethora of statistical features and graphical tools
▶ Interpreted language: offers command line interpreter (no compilation necessary)
▶ Nevertheless often quite fast (partially implemented in C and Fortran)
▶ Easy to extend via packages.4
▶ Open Source under GNU GPL v2
2
R is no general purpuse language!
3
According to R-manual: “”environment” is intended to characterize it as a fully planned and coherent
system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.”
4
Though R does a bad job at providing nice supporting tools. However, the community does via many
useful packages.
J. Bossek: A Gentle Introduction into R 4
C’mon! Yet another programming language?
5
[Link]
6J. Bossek: A Gentle Introduction into R 5
R vs. Python: [Link]
C’mon! Yet another programming language?
5
[Link]
6J. Bossek: A Gentle Introduction into R 5
R vs. Python: [Link]
C’mon! Yet another programming language?
5
[Link]
6J. Bossek: A Gentle Introduction into R 5
R vs. Python: [Link]
C’mon! Yet another programming language?
5
[Link]
6J. Bossek: A Gentle Introduction into R 5
R vs. Python: [Link]
C’mon! Yet another programming language?
5
[Link]
6J. Bossek: A Gentle Introduction into R 5
R vs. Python: [Link]
C’mon! Yet another programming language?
7
[Link]
J. Bossek: A Gentle Introduction into R 6
First impression
Achieve a lot with few lines of code:
> data(mtcars)
> X = mtcars[, c("qsec", "wt")]
> cl = kmeans(X, centers = 3, algorithm = "Lloyd")
> plot(X, pch = cl$cluster)
5
4
wt
3
2
16 18 20 22
qsec
J. Bossek: A Gentle Introduction into R 7
Editors with R support
The interactive shell is nice, but useless for larger projects. We want an editor with . . .
▶ . . . nice features: syntax-highlighting, code-completion etc.,
▶ possibility to save code in files and
▶ organize/maintain our data analysis project(s)
Nice editors
R-Studio powerful Integrated Development Environment (IDE) for R.
Sublime-Text Not an IDE! Very reduced, but minimalistic and lightning fast.
Visual Studio Code by Mircosoft; really good IDE with OKish R support.
Linux Depends on the flavour of you distribution, but your OS package manager
should help. E.g., on Ubuntu
sudo apt update
sudo apt install r-base
10
Unfortunately, the help page layout is from the past century.
11
Often documentations are full of details and might be overwhelming on first sight
J. Bossek: A Gentle Introduction into R 11
Data types and classes
Atomic data types / vector types
As in all other programming languages there are several data types:
numeric Real-valued numbers.
integer Integer numbers.
logical Boolean / logical values: TRUE and FALSE.
character Strings / concatenation of letters.
factor Special type of character which is internally stored as integer (for efficiency
reasons); used for categorical variables.
complex Complex numbers.
raw Not discussed here.
> class(x)
## [1] "numeric"
> class(x)
## [1] "character"
> x = c(FALSE, 1)
> x
## [1] 0 1
> class(x)
## [1] "numeric"
> class(x_num)
## [1] "numeric"
> class(x)
## [1] "numeric"
> rep(4:6, 3)
## [1] 4 5 6 4 5 6 4 5 6
Function Description
abs(x) Absolute value
sqrt(x) Square root
log(x) Natural logarithm (base e)
log10(x) Logarithm with base 10
exp(x) Exponential funciton e x
cos(x), sin(x), ... Trigonometric functions
ceiling(x) Round up: ceiling(6.475) is 7
floor(x) ound down: floor(6.489) is 6
trunc(x) Cut decimals: trunc(2.99) is 2
round(x, digits=n) Regular rounding: round(7.657, 2)
yields 7.67
Function Description
mean(x, [Link]=FALSE) Arithmetic mean of object x
sd(x) Standard deviation of object x
sd(x) Variance of object x
mad(x) Median absolute deviation of values in x
median(x) Median value of object x
quantile(x, probs) Quantiles where x is the numeric vector
whose quantiles are desired and probs is a
numeric vector with probabilities in [0, 1]
range(x) Range
sum(x) Sum
min(x) Minimum
max(x) Maximum
1. Generate the following vectors in R using seq, rep and combinations thereof:
> c(4,5,6,4,5,6,4,5,6)
> c(1,1,1,1,2,2,2,2,3,3,3,3)
> c(0, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
> c(1,1,2,2,3,3,8,9,8,9,8,9,8,9)
▶ Extract the 1st , 2nd and 4th elements and assign them to a new vector b
▶ Figure out how to calculate the length of a vector
▶ Extract the last 50% of the vector and assign it to a vector c
> (x < 3) && (y < 2) # only the first component is checked and no warning!
> all((x < 3) & (y < 2)) # are all component-wise comparisons true?
## [1] FALSE
> any(!((x < 3) & (y < 2))) # is at least one component-wise comparison false?
## [1] TRUE
Moreover, the longer version implements short-circuit evaluation, i.e., if we say x && y for
two scalars x and y, and x evaluates to TRUE, y is no evaluated at all.12
12
These semantics can by found in most languages.
J. Bossek: A Gentle Introduction into R 23
Missing data
R is build around data and unfortunately data is often missing /
Missing data is represented with the keyword NA in vectors13 .
> age = c(24, 23, 21, 21, NA, 31, NA, 19)
> age
## [1] 24 23 21 21 NA 31 NA 19
> mean(age) # most functions return NA if there is at least one NA in the data
## [1] NA
13
NA for Not Available.
J. Bossek: A Gentle Introduction into R 24
Vector subsetting by indexing
Access a subset of the vector elements by integer indices:
> x = c(10, 6, 3, 6, 3, 5, 9, 5, 15, 6)
> x[5] # single elments
## [1] 3
> x[-c(5, 7)] # all but the 5th and 7th element
## [1] 10 6 3 6 5 5 15 6
1. Consider the vector x = (49, 14, 25, 49, 14, 63, 65, 99, 56, 29). Create this vector in R
2. Create a logical vector where the i th entry is TRUE if the i th entry of x is equal to the
minimum value of x
3. The function rev reverts the order of a vector. Use it to revert the first half of x
4. Normalize/rescale the vector: i.e., from each element subtract the minimum and
divide by the difference of maximum and minimum value (functions min and max will
be useful)
> x[large]
## [1] 10 6 6 9 15 6
> x[idx_large]
## [1] 10 9 15
Function Description
sort(x) Sort elements
order(x) Indices of elements in sorted order
unique(x) Vector of unique elements (removes duplicates)
duplicated(x) Which elements of x are duplicates?
[Link](x) Index of smallest element
[Link](x) Index of largest element
which(x) Indices of elements in x which are TRUE
4. Create a vector which contains all unique elements of x in two different ways.
6. Create a vector y which contains the indices of unique elements in the sorted version
of x.
▶ Most attributes are dropped by operations (except for dim and/or names).
J. Bossek: A Gentle Introduction into R 32
Matrices
A matrix in R is like a matrix in mathematics:
> x = matrix(1:9, ncol = 3)
> x
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
> x[1, , drop = FALSE] # keep matrix structure even if we extract a single line
## [,1] [,2] [,3]
## [1,] 1 4 7
> y = 1:9
> attr(y, "dim") = c(3, 3) # "manually" transform to matrix
> y
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
> [Link](y)
## [1] TRUE
> mean(x)
## [1] 5
Function Description
A %*% B Matrix product A · B
A * B Element-wise product of matrices (Aij ·
Bij , 1 ≤ i, j, ≤ n)
t(A) Matrix transposition
eigen(A) Eigenvalues and Eigenvectors of A
solve(A) (regular) inverse A−1 of A
solve(A, b) Solution x of equation Ax = b
rowMeans(A) Row-wise mean values
colMeans(A) Column-wise mean values
rowSums(A) Row-wise sums
colSums(A) Column-wise sums
nrow(A), ncol(A) Number of rows/columns of A
dim(A) Dimension (i.e., rows and columns) of A
diag(A) Vector of diagonal elements of A
> l[["lectures"]][1]
## [1] "DA1"
> l2$degree
## [1] "PHD"
> x$name
## [1] "Max" "Sophie" "Jack" "Ted"
> # ...
> x$sex = c("M", "F", "M", "M") # add another variable
> head(x, n = 3) # show first n rows only
## id name grade sex
## 1 1 Max 5 M
## 2 2 Sophie 5 F
## 3 3 Jack 4 M
> x[(x$sex == "M") & (x$grade != 5), "name"] # get all males that failed the DA1 exam :(
## [1] "Jack"
> # ...
> x1 = x[(x$sex == "M") & (x$grade != 5), "name"] # Remember this?
>
> subset(x, sex == "M" & grade != 5, select = "name") # same result, nicer interface
## name
## 3 Jack
14
This is not limited to dataframes.
J. Bossek: A Gentle Introduction into R 42
Dataframes: import
Importing data from flat files in CSV-format:15
> # writing to a csv file using . as decimal point and ; as the "comma"
> data(mtcars)
> [Link](mtcars, file = "[Link]", [Link] = TRUE, sep = ";", dec = ".")
15
CSV = Comma Separated Values. Very common flat-file format for rectangular data.
J. Bossek: A Gentle Introduction into R 43
Dataframes: combining
2. Subset all observations where the horsepower is among the hightest 50% of the values
3. Subset all observations where the weight is in the interval [2, 3] and the number of
cylinders is at least 6
4. Create a data frame that contains the first and last 5 observations of mtcars
6. Add a new variable wtcat which takes two character values: ”heavy” if the weight
exceeds 4 and ”light” otherwise.
> opar = par(mfrow = c(1, 2), [Link] = 0.6) # some graphical parameter
> plot(party)
> levels(party)[1] = "Missing"
> plot(party)
3.0
3.0
1.5
1.5
0.0
0.0
− CDU Grüne SPD Missing CDU Grüne SPD
J. Bossek: A Gentle Introduction into R 51
Exercises
1. Create the following vector where both a dash and and empty string represent missing
values:
> x = c("a", "b", "c", "-", "", "c", "a", "b", "c", "-", "/")
3. Convert x to a factor
16
Default is alphabetic order.
J. Bossek: A Gentle Introduction into R 53
Exercises
4. Now we want to add a new variable power to the data. Use the function cut to
convert gross horsepower to the categorical variable power with factor levels (0, 150],
(150, 250] and (250, 350] (see argument breaks of cut)
6. Rename the factor levels to low, medium and high and make the factor ordered
> str(1:4)
## int [1:4] 1 2 3 4
> str(factor(c("good", "bad", "bad", "bad"), levels = c("good", "bad"), ordered = TRUE))
## [Link] w/ 2 levels "good"<"bad": 1 2 2 2
> str(mean)
## function (x, ...)
We can also execute scripts from the command line. This is powerful if you want to, e.g.,
▶ Automate processes or
▶ Call R through other tools.
Rscript myscript.R
Rscript myscript.R > [Link] # redirect output to file (on unix only)
R -e '[Link]("ggplot2", dependencies = TRUE)'
This works perfectly find in interactive mode, but not if we run a script via
Rscript myscript.R or source("myscript.R"). In these cases cases like the 2nd line
in the above listing will have no effect.
The following works as expected though:
> x = 10
> print(x)
## [1] 10
> x = 10
> y = 19.45353325
> s = "DA1 is awesome!"
> sprintf("x has the value %i", x) # %i for integer
## [1] "x has the value 10"
> sprintf("x = %i, y = %f", x, y) # %f for fixed point decimal notation [-][Link]
## [1] "x = 10, y = 19.453533"
17
Essentially a character with specific instructions embedded. The latter are substituted by the formatted
arguments passed after the format string in order of appearance.
J. Bossek: A Gentle Introduction into R 58
Basic R graphics
Basic R graphics: plot
R offers heaps of build-in plot functionality:
> data(mtcars)
> plot(mtcars$hp, mtcars$wt, xlab = "Gross horsepower", ylab = "Weight (in lbs)")
5
Weight (in lbs)
4
3
2
Gross horsepower
J. Bossek: A Gentle Introduction into R 59
Basic R graphics: barplot
> tab = table(mtcars$cyl)
> barplot(tab, [Link] = 0.7, main = "Distribution of the nr. of cylinders")
14
12
10
8
6
4
2
0
4 6 8
Histogram of mtcars$disp
7
6
5
Frequency
4
3
2
1
0
Displacement
Histogram of mtcars$disp
5
4
Frequency
3
2
1
0
Displacement
7
6
5
Petal length
4
3
2
1
Sepal length
7
6
5
Petal length
4
3
setosa
2
versicolor
virginica
1
Sepal length
50
35
30
30
40
25
25
20
30
Frequency
Frequency
20
15
15
20
10
10
10
5
5
0
0
4 5 6 7 8 0.0 0.5 1.0 1.5 2.0 2.5 setosa versicolor virginica
18
PDF = Portable Data Format; vector-based format.
19
PNG = Portable Network Graphics; pixel-based format.
20
TIFF = Tag Image File Format.
J. Bossek: A Gentle Introduction into R 66
Exercises
1. Install and load the ggplot2 package with
> [Link]("ggplot2", dep = TRUE)
> library(ggplot2)
21
The data set contains 53 940 observations. It may take some time to render plots for that many
observations. Try it!
J. Bossek: A Gentle Introduction into R 67
Basic R graphics: conclusion
22
It is not the holy grail, but way better and visually appealing.
J. Bossek: A Gentle Introduction into R 68
Control flow
Conditional statements
Conditions are elemantary building blocks of (procedural) programming: do something if
certain logical conditions hold and do something else if they do not hold.
> x = c(1, 2, 3, 3, 2, 1, 5, 3)
> (x %% 2) # modulo operator -> rest of integer division with 2
## [1] 1 0 1 1 0 1 1 1
> if (c()) 1
+ print(i)
+ if (i >= 8)
+ break
+ }
## [1] 1
## [1] 2
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
23
If allocation takes linear time, a linear number of re-allocations takes qaudratic time.
J. Bossek: A Gentle Introduction into R 77
Loops are slow in R, aren’t they?
Oftentimes you will read: avoid loops in R. They are slow as hell!
Golden rules to not slow down your R code with loops
▶ Use R’s vectorization. I.e., if there exists a build-in vectorized function, use it!
▶ Avoid using c or cbind/rbind in loops to grow objects
; R needs to re-allocate memory in every iteration.23
▶ Instead, try to pre-allocate memory in advance.
23
If allocation takes linear time, a linear number of re-allocations takes qaudratic time.
J. Bossek: A Gentle Introduction into R 77
Loops are slow in R, aren’t they?
Oftentimes you will read: avoid loops in R. They are slow as hell!
Golden rules to not slow down your R code with loops
▶ Use R’s vectorization. I.e., if there exists a build-in vectorized function, use it!
▶ Avoid using c or cbind/rbind in loops to grow objects
; R needs to re-allocate memory in every iteration.23
▶ Instead, try to pre-allocate memory in advance.
> x = rnorm(100000) # data generation: N(0, 1), i.e., normally distributed randoms
> n = length(x)
>
> [Link](cumsum(x)) # build-in C-based function (ultra fast)
## user system elapsed
## 0 0 0
> [Link]({
+ cs = numeric(n)
+ cs[1] = x[1]
+ i = 2
+ while (i <= n) {
+ cs[i] = cs[i - 1] + x[i]
+ i = i + 1
+ }
+ })
## user system elapsed
## 0.008 0.000 0.008
▶ Functions encapusulate code that solves some interesting sub-task and is general
enough to be reused.
▶ Allow for automation of common tasks.
▶ It is much like a math. function. E.g.
n
X
f : Rn × Rn → R with f (x, y ) 7→ xi · yi .
i=1
▶ The good thing is: once we have a useful function, we no longer care about its exact
implementation, but only about the function interface/signature. I.e., what does the
function expect as input and what does it return as output?
▶ R functions are objects!
; Functions can be assigned to variables, stored in vectors, passed down to other
functions as arguments etc.
J. Bossek: A Gentle Introduction into R 82
Functions: definition
Pn
Let’s write a function for our problem: i=1 xi · yi .
> sumprod = function(x, y) {
+ r = sum(x * y)
+ return(r)
+ }
>
> sumprod(c(29, 10, 4), c(10, 24, 53))
## [1] 742
> x
## [1] 1
> f = function() {
+ x = x + 1
+ x
+ }
> f()
## [1] 2
> x
## [1] 1
25
Object names x for the first input or variables i, j, k, l for indices are quite common.
J. Bossek: A Gentle Introduction into R 84
Lazy evaluation
Lazy evaluation means that an expression is evaluated only if the expression (calculation,
value of object) is actually used.
> lazyfun = function(x, y) {
+ if (x < 10)
+ x + 10
+ else
+ x + y[1]
+ }
>
> lazyfun(10) # works since y is never used and thus not evaluated in lazyfun
Powerful tool! Helps to avoid costly calculations if the result won’t be used.
> lazyfun(4)
## [1] 14
> lazyfun(20) # errors, since y not passed, but function tries to use it
> x = 1:1000000
> if (x[1] == 1 || (sqrt(sum(x^2)) > 500)) { # costly second condition skipped
+ print("Second condition not checked.")
+ }
## [1] "Second condition not checked."
26
Similar to pipe operator in unix command line.
27
We will make extensive use of piping later.
J. Bossek: A Gentle Introduction into R 89
Exercises
1. Write a function means(x) which expects a numeric matrix x and returns a vector
where the i th entry corresponds to the arithmetic mean of the i th column of x.
2. Modify means such that it expects another argument of. If of="column" the
function shall behave as in (1). If of="row" the row-wise means shall be calculated
instead.
3. The Fibonacci-numbers28 is a famous recursive infinite sequence of numbers following
the building rule
28
[Link]
J. Bossek: A Gentle Introduction into R 90
The *apply-function family
Often we have an n-dim. object x = (x1 , x2 , . . . , xn ) ∈ Dn and a function f : D → E that
we want to apply to each element of x:
E.g.
▶ Apply a function to each element of a vector.
▶ Apply a function to each column (or row) of a matrix.
▶ Apply a function to each matrix in a list.
▶ etc.
29
[Link]
J. Bossek: A Gentle Introduction into R 94
Code style
Actually, the R interpreter does not care whether your code looks like this ...
> x=c(24, 23, 21, 21, NA, 31, NA, 19)
> f2=function(x, [Link]=FALSE){
+ if([Link]){x = x[[Link](x)]}
+ return(sum(x)/length(x))}
> f(x)
... or ...
> age = c(24, 23, 21, 21, NA, 31, NA, 19)
> average = function(x, [Link] = FALSE) {
+ if([Link]) { # drop NAs if argument is TRUE
+ x = x[[Link](x)]
+ }
+ return(sum(x) / length(x))
+ }
> average(age)
31
Basically software libraries.
J. Bossek: A Gentle Introduction into R 96
Packages (cont.)
Once a package is installed it can be loaded and all exported functions and datasets can
be used:
> library(smoof)
> # the prefix 'smoof::' is not necessary, but useful to keep track where the function
> # is implemented
> fn = smoof::makeAckleyFunction(dimensions = 2)
> plot(fn, [Link] = TRUE, [Link] = FALSE) 2−d Ackley Function
30
20
20
10 16
12
4
x2
8
10
14
−10
18
−20
−30
Types of parallelism
Implicit parallelism parallelism hidden from user, system abstracts it away.
Explicit parallelism user need to handle paralellism explicitely by using special directives.
Implicit parallelism?
Parallelism is hidden from the user, no user-requests/directives needed. Thus parallelism is
automatically exploited.
▶ Paralellized computation of mathematical functions in Blas library.
▶ Installation on windows systems is simple: replace [Link] with compiled one.
▶ Installation on unix systems: configure R with -with-blas flag and compile
afterwards; fastest implementation is OpenBlas (hand optimized assembler code).
Explicit parallelism?
Parallelism is activated by the user calling specific directives.
> library(parallel)
> [Link] = 2 # nuumber of cores
>
> x = 1:100
> x = split(x, rep (1:[Link], each = length(x) / [Link]))
> str(x)
## List of 2
## $ 1: int [1:50] 1 2 3 4 5 6 7 8 9 10 ...
## $ 2: int [1:50] 51 52 53 54 55 56 57 58 59 60 ...
> sum(y)
## [1] 5050
## Error in requirePackages(package, why = stri paste("learner", id, sep = " "), : For learner [Link] please install the
following packages: clue
## Timing stopped at: 0.001 0 0.009
## Warning in mclapply(1:[Link], f): all scheduled cores encountered errors in user code
## user system elapsed
## 0.003 0.024 0.024
32
The difference in runtime is almost neglegible in this example since mtcars is a very small data set.
However, if on larger data one run of k-means takes a minute you will experience a massive, close to
optimal speed-up if you perform at most as many runs as there are CPU cores.
J. Bossek: A Gentle Introduction into R 103
Explicit parallelism: parallelMap
▶ Major drawback of methods seen so far: backend change requires change of code in
general /
▶ R package parallelMap33 is another wrapper
▶ Single function to learn: parallelMap
▶ No need to change code if backend changes
▶ Configurable via options
▶ Support for most important parallelization modes, i. e., multicore machines, socket
mode, MPI and HPC
33
[Link]
J. Bossek: A Gentle Introduction into R 104
Explicit parallelism: parallelMap (cont.)
> library(parallelMap)
> # start in socket mode
> parallelStartSocket(cpus = 2L)
> f = function(i) {
+ res = summary(runif(1e6))
+ }
> parallelMap(f, 1:2) # like lapply(1:2, f)
> parallelStop()
1. Gareth James, Daniela Witten, Trevor John Hastie & Robert Tibshirani (2021). An
Introduction to Statistical Learning. 2nd Edition. Springer. (James et al. 2013)⋆
; Brief R intro; R labs at the end of each chapter.
2. Hadley Wickham & Garrett Grolemund (2017). R for Data Science: Import, Tidy,
Transform, Visualize, and Model Data. O’Reilly. (Wickham and Grolemund 2017)
4. Hadley Wickham (2014). Advanced R. Chapman & Hall/CRC The R Series. Taylor &
Francis. (Wickham 2014)
5. Hadley Wickham (2015). R Packages. 1st Edition. O’Reilly Media. (Wickham 2015)
Todays content
R introduction (basic concepts, loops, functions etc.)
35
URL: [Link]
36
URL: [Link]
J. Bossek: A Gentle Introduction into R 109
Wrap-Up
Todays content
R introduction (basic concepts, loops, functions etc.)
Your task(s)
▶ Download and install both R35 and R Studio36 .
▶ Work through the R-intro in James et al. 2013.
▶ Work through the presentation slides and do the many exercises.
▶ Work on exercise sheet 02.
35
URL: [Link]
36
URL: [Link]
J. Bossek: A Gentle Introduction into R 109
References I
Wickham, Hadley (2009). ggplot2: elegant graphics for data analysis. Springer New York. isbn:
978-0-387-98140-6.
— (2014). Advanced R. Chapman & Hall/CRC The R Series. Taylor & Francis. isbn: 9781466586963.
url: [Link]
James, Gareth et al. (2013). An Introduction to Statistical Learning: with Applications in R. Springer.
Wickham, Hadley and Garrett Grolemund (Jan. 2017). R for Data Science: Import, Tidy, Transform,
Visualize, and Model Data. 1st ed. O’Reilly Media. isbn: 1491910399.
Wickham, Hadley (2015). R Packages. 1st. O’Reilly Media, Inc. isbn: 1491910593.