class: center, middle, inverse, title-slide .title[ # 03 Functions ] .subtitle[ ## Elements of R e R Markdown ] .author[ ### Claudio Zandonella ] --- class: size-small # Defining Functions .pull-left-50[ ```r name_function <- function(<argument-1>, <argument-2>, ...){ <body-function> return(<result>) } ``` ```r greetings <- function(name){ res <- paste("Hello", name) return(res) } greetings("Claudio") ## [1] "Hello Claudio" ``` ] .pull-right-50[ ### Arguments - `function(x){...}`, argument `x` is require with not default value - `function(x = 0){...}`, argument `x` is require with default value `0` - `function(...){...}`, argument `...` allows arbitrary number of arguments ### Interactive - `cat()` and `print()`, print objects in console - `readline(prompt = "...")`, get user's response - `menu(<choices>)`, user select answer from menu ] --- class: size-small # Conditional Programming .pull-left-50[ ### If statement ```r if (<test>) { <body> } ``` ```r is_negative <- function(x){ if (x < 0) { print(paste(x, "is negative")) } } is_negative(-2) ## [1] "-2 is negative" ``` ] .pull-right-50[ ### If else statement ```r if (<test>) { <body> } else { <body> } ``` ```r tell_sign <- function(x){ if (x < 0) { print(paste(x, "is negative")) } else { print(paste(x, "is possitive")) } } is_negative(2) ``` ] --- class: size-small # Iterative Programming .pull-left-50[ ### For loop ```r for (i in c(...)) { <body> } ``` ```r call_friends <- function(friends){ for( friend in friends){ cat("Hello ", friend, "!\n", sep = "") } } call_friends( friends = c("Emy", "John", "Mat") ) ## Hello Emy! ## Hello John! ## Hello Mat! ``` ] .pull-right-50[ ### While loop ```r while (<test>) { <body> } ``` ```r countdown <- function(count){ while(count > 0){ cat(count, "\n") count <- count - 1 } cat("Start!\n") } countdown(count = 3) ## 3 ## 2 ## 1 ## Start! ``` ] --- class: size-small # Apply Family .pull-left-50[ ```r apply(X = , MARGIN = , FUN = ) ``` ```r (X <- matrix(1:12, ncol = 4)) ## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12 # By rows apply(X, MARGIN = 1, FUN = sum) ## [1] 22 26 30 # By columns apply(X, MARGIN = 2, FUN = sum) ## [1] 6 15 24 33 # Custom function apply(X, MARGIN = 1, function(x){ round(sd(x)/mean(x), 2) }) ## [1] 0.70 0.60 0.52 ``` ] .pull-right-50[ ### Other - `tapply()`, apply a function to each group of values given by a combinations of factors - `lapply()`, apply a function over all elements of a lists (return a list) - `sapply()`, apply a function over all elements of a lists (return a vector or a list) - `vapply()`, apply a function over all elements of a lists (return a vector) - `replicate()`, repeatedly apply a function. ] --- class: size-small # Probability Distributions .pull-left-50[ ### Normal Disribution - `dnorm(x, mean = 0, sd = 1)`, get the density given the quantile - `pnorm(q, mean = 0, sd = 1)`, get the probability given the quantile - `qnorm(p, mean = 0, sd = 1)`, get the quantile given the probability - `rnorm(n, mean = 0, sd = 1)`, sample `n` values ```r dnorm(0, mean = 0, sd = 1) ## [1] 0.3989423 pnorm(0, mean = 0, sd = 1) ## [1] 0.5 qnorm(0, mean = 0, sd = 1) ## [1] -Inf rnorm(0, mean = 0, sd = 1) ## numeric(0) ``` ] .pull-right-50[ ### Other Distributions <table class="table table-striped table-hover" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Discrete </th> <th style="text-align:left;"> Continuous </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> <code class="remark-inline-code">*binom()</code>, Binomial </td> <td style="text-align:left;"> <code class="remark-inline-code">*unif()</code>, Uniform </td> </tr> <tr> <td style="text-align:left;"> <code class="remark-inline-code">*pois()</code>, Poisson </td> <td style="text-align:left;"> <code class="remark-inline-code">*chisq()</code>, Chi-Square </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> <code class="remark-inline-code">*t()</code>, Student-t </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> <code class="remark-inline-code">*f()</code>, F </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> <code class="remark-inline-code">*gamma()</code>, Gamma </td> </tr> </tbody> </table> ### Randomness ```r # Random Number Generation set.seed(seed = <value>) # Smaple and permutations sample(x = , size = , replace = , prob = ) ``` ] --- class: end, middle, center # Thanks!