Background? R and Rstudio installed? What do you know about R?
Contact Information:
Jens Klenke Jens.Klenke@vwl.uni-due.de
Martin Arnold Martin.Arnold@vwl.uni-due.de
Slides, exercises and announcements will be provided on:
Background? R and Rstudio installed? What do you know about R?
There will be two graded assignments.
You will have to work on a final group project which focuses on one or more of the topics discussed in the course.
The final grade is a weighted average of the grades for the group project (60%), its presentation (20%) and the assignments (20%).
Advantages:
Disadavantages:
How to install packages from CRAN?
install.packages("MASS")
to install the package MASS
from CRAN.MASS
How to install packages from CRAN?
install.packages("MASS")
to install the package MASS
from CRAN.MASS
How to install packages from Github?
devtools
from CRAN.E.g., use devtools::install_github("tidymodels/broom")
to install the broom
package from Github.
If you want to find out how a package works, search for
How to install packages from CRAN?
install.packages("MASS")
to install the package MASS
from CRAN.MASS
How to install packages from Github?
devtools
from CRAN.E.g., use devtools::install_github("tidymodels/broom")
to install the broom
package from Github.
If you want to find out how a package works, search for
install.packages("broom")library(broom)vignette("broom")
Which functionality does the broom package provide?
If you have specific questions about your code or an error message look on
The chances are high that somebody else has already asked the same question and it has already been solved.
If you have specific questions about your code or an error message look on
The chances are high that somebody else has already asked the same question and it has already been solved.
If you don't find an answer you can ask a question yourself. What sounds easy is harder then one would think. Most importantly you have to provide a reproducible example.
Bad questions: https://stackoverflow.com/questions/58302656/how-can-i-set-equals-function-in-objective-of-solver-in-r-python https://stackoverflow.com/questions/58121493/update-a-coordinate-dataframe-in-shiny
Good questions:
https://stackoverflow.com/questions/38777337/ggplot-ribbon-cut-off-at-y-limits/38777929#38777929
https://stackoverflow.com/questions/53241120/how-to-use-openblas-lapacke-together-with-rcpp
setwd()
. This is problematic if you want to
share your code or work from different machines on the same project. setwd()
. This is problematic if you want to
share your code or work from different machines on the same project. setwd()
you open the project. The working path is
automatically set to the folder where the .Rproj
file lives in. Shortcut | Action |
---|---|
Alt + Shift + k | list all shortcuts |
Cmd/Ctrl + Shift + F10 | restart R |
Cmd/Ctrl + Shift + S | rerun current script |
F1 | help page |
Ctrl + l | clears the console |
Shortcut | Action |
---|---|
Alt + Shift + k | list all shortcuts |
Cmd/Ctrl + Shift + F10 | restart R |
Cmd/Ctrl + Shift + S | rerun current script |
F1 | help page |
Ctrl + l | clears the console |
Shortcut | Action |
---|---|
Alt + Shift + k | list all shortcuts |
Cmd/Ctrl + Shift + F10 | restart R |
Cmd/Ctrl + Shift + S | rerun current script |
F1 | help page |
Ctrl + l | clears the console |
We will use some of this later on.
We assume that you know the basic data structures in R.
Dimensions | Homogeneous | Heterogeneous |
---|---|---|
1 | Atomic Vector | List |
2 | Matrix | Data Frame |
n | Array |
We assume that you know the basic data structures in R.
Dimensions | Homogeneous | Heterogeneous |
---|---|---|
1 | Atomic Vector | List |
2 | Matrix | Data Frame |
n | Array |
You should also be familiar with the basic data types (character, double, integer, logical).
We assume that you know the basic data structures in R.
Dimensions | Homogeneous | Heterogeneous |
---|---|---|
1 | Atomic Vector | List |
2 | Matrix | Data Frame |
n | Array |
You should also be familiar with the basic data types (character, double, integer, logical).
Task
Answers Task
object.size(1:1000)object.size(as.numeric(1:1000))char_vector <- c("Hi", "there")typeof(char_vector)class(char_vector)num_matrix <- matrix(rnorm(4), ncol = 2)typeof(num_matrix)class(num_matrix)is.numeric(num_matrix)plus <- function(a, b){ if(!is.numeric(a)) stop("a is not numeric") if(!is.numeric(b)) stop("b is not numeric") a + b}plus(1, "a")
You should know how to use the common subsetting operators [
, [[
and $
.
Have you ever encountered @
?
You should know how to use the common subsetting operators [
, [[
and $
.
Have you ever encountered @
?
[
, [[
, and $
when applied to a list? drop = FALSE
? [] = always returns object of same class [[]] = can extract one element from list or data frame list_a <- list(a = 2, b = "hallo") list_a["a"] list_a[["a"]] list_a$a
[ selects sub-lists. It always returns a list; if you use it with a single positive integer, it returns a list of length one. [[ selects an element within a list. $ is a convenient shorthand: x$y is equivalent to x[["y"]].
Use drop = FALSE if you are subsetting a matrix, array, or data frame and you want to preserve the original dimensions. You should almost always use it when subsetting inside a function.
## Dropm1 <- matrix(1:4, nrow = 2)m2 <- matrix(1:4, nrow = 2)matrix_prod <- function(A, B){ B %*% t(A)}matrix_prod(m1[1, ], m2)matrix_prod(m1[1, , drop = FALSE], m2)## Example for S4 classN <- 200x.vec <- as.vector(fGarch::garchSim(fGarch::garchSpec(rseed = 1985), n = N)[,1])garch11 <- fGarch::garchFit(~ garch(1,1), data = x.vec, trace = FALSE)isS4(garch11)
The basic control flows are conditionals (choices) and loops.
Task
if
and ifelse()
?What will be the value of y
in the following code if x
is TRUE
? What if x
is FALSE
? What if x
is NA
?
y <- if(x) 3
if("2") 3if(2) 3for(i in 1:3){ print(i)}i <- 1while(i < 4){ print(i) i <- i + 1}i <- 1repeat{ print(i) i <- i + 1 if(i > 3) break}
Task
x
and returns the square of x
. Task
x
and returns the square of x
. Task
x
and returns the square of x
. f1 <- function() xx <- "global"f2 <- function(){ x <- "local_f2" f1()}f2()
R’s lexical scoping follows four primary rules:
Explaniation on the following slides
x <- 3square_1 <- function() x^2square_1()
x <- 3square_1 <- function() x^2square_1()
x <- 3square_2 <- function(){ x <- 2 foo <- function() x^2 foo()}square_2()
x <- 3square_1 <- function() x^2square_1()
x <- 3square_2 <- function(){ x <- 2 foo <- function() x^2 foo()}square_2()
Example 1 works fine x will be not overwritten Exmaple two -> x will be overwritten in the function
x <- 3square_2 <- function(){ x <- 2 foo <- function() x^2 foo()}square_3 <- function(x){ square_2 <- 5 square_2()}square_3()
x <- 3square_2 <- function(){ x <- 2 foo <- function() x^2 foo()}square_3 <- function(x){ square_2 <- 5 square_2()}square_3()
square_4()
? What will happen the second time?x <- 2square_4 <- function(){ (x <- x^2)}square_4()square_4()
square_4()
? What will happen the second time?x <- 2square_4 <- function(){ (x <- x^2)}square_4()square_4()
f1()
. Compare it to the main rule of lexical scoping.x <- 1f1 <- function() xf1()x <- 2 f1()
f1()
. Compare it to the main rule of lexical scoping.x <- 1f1 <- function() xf1()x <- 2 f1()
lazy_function <- function(x){ 10}lazy_function()
lazy_function <- function(x){ 10}lazy_function()
Why does this not induce an error?
lazy_function <- function(x){ 10}lazy_function()
Why does this not induce an error?
lazy_function <- function(x){ x}lazy_function()
day_oneday_1
day_oneday_1
# Badfirst_day_of_the_monthdayonedjm1T <- FALSEc <- 10mean <- function(x) sum(x)
=
, +
, -
, <-
, etc.) and =
in a function call.average <- mean(feet / 12 + inches, na.rm = TRUE)
average<-mean(feet/12+inches,na.rm=TRUE)
=
, +
, -
, <-
, etc.) and =
in a function call.average <- mean(feet / 12 + inches, na.rm = TRUE)
average<-mean(feet/12+inches,na.rm=TRUE)
:
, ::
and :::
.x <- 1:10base::get
x <- 1 : 10base :: get
<-
).list( total = a + b + c, mean = (a + b + c) / n)
<-
).list( total = a + b + c, mean = (a + b + c) / n)
if (debug) do(x)diamonds[5, ]
if ( debug ) do(x) # No spaces around debugx[1,] # Needs a space after the commax[1 ,] # Space goes after comma not before
if (y < 0 && debug) { message("Y is negative")}if (y == 0) { log(x)} else { y ^ x}
if (y < 0 && debug) { message("Y is negative")}if (y == 0) { log(x)} else { y ^ x}
if (y < 0 && debug) {message("Y is negative") }if (y == 0) { log(x)} else { y ^ x}
if (y < 0 && debug) message("Y is negative")
if (y < 0 && debug) message("Y is negative")
<-
, not =
, for assignment.x <- 5
x = 5
#
. -
and =
to break up your file into easily readable chunks.#
. -
and =
to break up your file into easily readable chunks.# Load data ---------------------------# Plot data ---------------------------
df_list
to one data frame. The result should be only a single line of code. df_list <- lapply(0:4, function(j) data.frame(c1 = (1:5) + 5 * j, c2 = letters[(1:5) + 5 * j] ) )
mtcars[mtcars$cyl = 4, ]mtcars[-1:4, ]mtcars[mtcars$cyl <= 5]mtcars[mtcars$cyl == 4 | 6, ]
df[is.na(df)] <- 0
do? while
and a repeat
loop doing the same as for(i in LETTERS[1:10]){ print(i)}
for
loop doing the same ascount <- 0repeat{ x <- sample(1:6, 1) count <- count + 1 if(x == 6) break}print(count)
# Number of applesi <- 100for (i in 1:3) {} paste("The number of apples is", i)
df <- mtcarslin_mod <- function(){ lm(y ~ . ,data = df)}simple_lin_mod <- function(data, y, x){ df <- data[ ,c(y, x)] names(df) <- c("y", "x") fast_lm()}simple_lin_mod(df, "mpg", "cyl")
Contact Information:
Jens Klenke Jens.Klenke@vwl.uni-due.de
Martin Arnold Martin.Arnold@vwl.uni-due.de
Slides, exercises and announcements will be provided on:
Background? R and Rstudio installed? What do you know about R?
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |