[1] 1
[1] 1 2 6 20 30 40 100 200 300
[1] 1 2 3 4 5 6 7 8 9 10
Block 2, Session 1: R Programming 101
“Don’t get bored by the process.”
– Chris Bosh (11x NBA Allstar, 2x NBA champion) on The Knowledge Project podcast.
📜 Scripts (Experimental)
📜 Scripts (Experimental)
⬇️
♻️ Functions (Reusable)
📜 Scripts (Experimental)
⬇️
♻️ Functions (Reusable)
⬇️
📦 Packages (Deployable)
Be aware of lexical scoping (look up of variables in parent environments when they can’t be found in the current environment) when working with functions.
The section on environment basics in Hadley Wickham’s Advanced R is a good, applied introduction.
How R Searches and Finds Stuff goes much further down the rabbit hole.
name_of_the_function <- function(parameter_1, parameter_2){
# function body
s <- parameter_1 + parameter_2
# R does *need* a return statement
# it returns the last statement that is not
# assigned to a variable using the assignment operator `<-`
return(s)
# NOTE an R function can only return ONE object (!)
}
set.seed(123)
d1 <- rnorm(1000)
d2 <- rnorm(1000)
d1_mean <- mean(d1)
d1_sd <- sd(d1)
d1_q <- quantile(d1)
desc_stats_d1 <-
list(d1_mean = d1_mean,
d1_sd = d1_sd,
d1_q = d1_q)
d2_mean <- mean(d2)
d2_sd <- sd(d2)
d2_q <- quantile(d2)
desc_stats_d2 <-
list(d2_mean = d2_mean,
d2_sd = d2_sd,
d2_q = d2_q)
set.seed(123)
d1 <- rnorm(1000)
d2 <- rnorm(1000)
d1_mean <- mean(d1)
d1_sd <- sd(d1)
d1_q <- quantile(d1)
desc_stats_d1 <-
list(d1_mean = d1_mean,
d1_sd = d1_sd,
d1_q = d1_q)
d2_mean <- mean(d2)
d2_sd <- sd(d2)
d2_q <- quantile(d2)
desc_stats_d2 <-
list(d2_mean = d2_mean,
d2_sd = d2_sd,
d2_q = d2_q)
set.seed(123)
d1 <- rnorm(1000)
d2 <- rnorm(1000)
d1_mean <- mean(d1)
d1_sd <- sd(d1)
d1_q <- quantile(d1)
desc_stats_d1 <-
list(d1_mean = d1_mean,
d1_sd = d1_sd,
d1_q = d1_q)
d2_mean <- mean(d2)
d2_sd <- sd(d2)
d2_q <- quantile(d2)
desc_stats_d2 <-
list(d2_mean = d2_mean,
d2_sd = d2_sd,
d2_q = d2_q)
#' Create Basic Descriptive Statistics
#'
#' Creates means, standard deviations and
#' default quantiles from an numeric input vector.
#'
#' @param distr numeric vector
#' @export
create_basic_desc <- function(distr){
out <- list(
mean = mean(distr),
sd = sd(distr),
quantiles = quantile(distr)
)
out
}
me: “How to name things?”
me: “How to name things?”
Google: “About 5’260’000’000 results (0.71 seconds)”
frequently occurs in: file and folder names, function names in R and Python.
frequently occurs in: class names (mostly UpperCamelCase).
frequently occurs in: folder, file and repository names.
Anonymous Functions
$iris
[1] 150
$mtcars
[1] 32
Anonymous Functions
$iris
[1] "The dataset contains 150 observations."
$mtcars
[1] "The dataset contains 32 observations."
for more elaborate examples, such as function factories, take a read the Functional Programming Chapter of Hadley Wickham’s book.
Note how R is dynamically typed.
Without Safety Net
“Error in a + b : non-numeric argument to binary operator Calls: […]”
With Safety Net
Hacking for Science by Dr. Matthias Bannert is licensed under CC BY-NC-SA 4.0