class: center, middle, inverse, title-slide # R Basics ### Applied Machine Learning with R
The R Bootcamp @ AMLD
### November 2021 --- layout: true <div class="my-footer"> <span style="text-align:center"> <span> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/by-sa.png" height=14 style="vertical-align: middle"/> </span> <a href="https://therbootcamp.github.io/"> <span style="padding-left:82px"> <font color="#7E7E7E"> www.therbootcamp.com </font> </span> </a> <a href="https://therbootcamp.github.io/"> <font color="#7E7E7E"> Applied Machine Learning with R @ AMLD | November 2021 </font> </a> </span> </div> --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><mono><-</mono> creates/changes objects</span></li> <li class="m3"><span>Everythings happens through functions</span></li> <li class="m4"><span>Functions have (default) arguments</span></li> <li class="m5"><span>Arguments expect classes</span></li> <li class="m6"><span>Find help using <mono>?</mono></span></li> <li class="m7"><span>Chain functions with the pipe</span></li> <li class="m8"><span>Functions exist in Packages</span></li> <li class="m9"><span>Data lives in data frames</span></li> <li class="m10"><span>Three+ data types</span></li> <li class="m11"><span>Formulae</span></li> </ul> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1"><span><high>The assignment operator <mono><-</mono></high></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <br> <p align="center"> <img src="image/assignment.png"> </p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><high><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # An object called one_two_three one_two_three <- c(1, 2, 3) # Print the object print(one_two_three) ``` ``` ## [1] 1 2 3 ``` ```r # Print the object one_two_three ``` ``` ## [1] 1 2 3 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><high><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # An object called one_two_three one_two_three <- c(1, 2, 3) # Calculate arithmetic mean mean(one_two_three) ``` ``` ## [1] 2 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><high><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # An object called one_two_three one_two_three <- c(1, 2, 3) # Calculate arithmetic mean result <- mean(one_two_three) # Use the result result * 10 ``` ``` ## [1] 20 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><high><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # An object called one_two_three one_two_three <- c(1, 2, 3) # Print the object one_two_three ``` ``` ## [1] 1 2 3 ``` ```r # Add 100 one_two_three + 100 ``` ``` ## [1] 101 102 103 ``` ```r # Print the object one_two_three ``` ``` ## [1] 1 2 3 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2"><span><high><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # An object called one_two_three one_two_three <- c(1, 2, 3) # Print the object one_two_three ``` ``` ## [1] 1 2 3 ``` ```r # Add 100 and re-assign one_two_three <- one_two_three + 100 # Print the object one_two_three ``` ``` ## [1] 101 102 103 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3"><span><high>Everythings happens through functions</high></span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <br><br> <p align="center"><img src="image/input_output.png" height="300"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3"><span><high>Everythings happens through functions</high></span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # Function c() one_two_three <- c(1, 2, 3) # Function `+`() one_two_three + 100 ``` ``` ## [1] 101 102 103 ``` ```r # Function print() one_two_three ``` ``` ## [1] 1 2 3 ``` ```r # Function mean() mean(one_two_three) ``` ``` ## [1] 2 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4"><span><high>Functions have (default) arguments</high></span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # No argument mean() ``` ``` ## Error in mean.default(): argument "x" is missing, with no default ``` ```r # One (necessary) argument mean(c(1, 2, 3)) ``` ``` ## [1] 2 ``` ```r # Add a missing value mean(c(1, 2, 3, NA)) ``` ``` ## [1] NA ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4"><span><high>Functions have (default) arguments</high></span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # No argument mean() ``` ``` ## Error in mean.default(): argument "x" is missing, with no default ``` ```r # One (necessary) argument mean(c(1, 2, 3)) ``` ``` ## [1] 2 ``` ```r # Change the default to handle missing mean(c(1, 2, 3, NA), na.rm = TRUE) ``` ``` ## [1] 2 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4"><span><high>Functions have (default) arguments</high></span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # Mean of c(1, 2, 3) mean(c(1, 2, 3)) ``` ``` ## [1] 2 ``` ```r # Mean of c("1", "2", "3") mean(c("1", "2", "3")) ``` ``` ## Warning in mean.default(c("1", "2", "3")): argument is not numeric or logical: returning NA ``` ``` ## [1] NA ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5"><span><high>Arguments expect classes</high></span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # Class of c(1, 2, 3) class(c(1, 2, 3)) ``` ``` ## [1] "numeric" ``` ```r # Class of c("1", "2", "3") class(c("1", "2", "3")) ``` ``` ## [1] "character" ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6"><span><high>Find help using <mono>?</mono></high></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r ?mean ``` <p align="center"><img src="image/help_mean.png" width="500"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6"><span><high>Find help using <mono>?</mono></high></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r ?mean ``` <p align="center"><img src="image/help_usage.png" width="500"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6"><span><high>Find help using <mono>?</mono></high></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # Mean of c(1, 2, 3) mean(c(1, 2, 3)) ``` ``` ## [1] 2 ``` ```r # Mean of c(TRUE, FALSE, TRUE) mean(c(TRUE, FALSE, TRUE)) ``` ``` ## [1] 0.6667 ``` ```r # Mean of c("1", "2", "3") mean(c("1", "2", "3")) ``` ``` ## Warning in mean.default(c("1", "2", "3")): argument is not numeric or logical: returning NA ``` ``` ## [1] NA ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7"><span><high>Chain functions with the pipe</high></span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # score <- c(8, 4, 6, 3, 7, 3) score ``` ``` ## [1] 8 4 6 3 7 3 ``` ```r # Mittelwert: Base-R-style mean(score) ``` ``` ## [1] 5.167 ``` ```r # Mittelwert: dplyr-style score %>% mean() ``` ``` ## [1] 5.167 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7"><span><high>Chain functions with the pipe</high></span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # score <- c(8, 4, 6, 3, 7, 3) score ``` ``` ## [1] 8 4 6 3 7 3 ``` ```r # Mittelwert: Base-R-style round(mean(score), 2) ``` ``` ## [1] 5.17 ``` ```r # Mittelwert: dplyr-style score %>% mean() %>% round(2) ``` ``` ## [1] 5.17 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7"><span><high>Chain functions with the pipe</high></span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <p align="center"><img src="image/pipe_explanation_kn.png" width="500"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8"><span><high>Functions exist in Packages</high></span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <p align="center"><img src="image/packages.png" height="400"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8"><span><high>Functions exist in Packages</high></span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <high>Install</high> packages <high>once</high> using `install.packages()` ```r install.packages("tidyverse") ``` <br2> <high>Attach</high> existing packages <high>every time</high> using `library()` ```r library(tidyverse) ``` <p align="left"><img src="image/tidyverse.png" height="180"></p> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span><high>Data lives in data frames</high></span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right55[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/main_objects.png"></img> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10"><span><high>Three+ data types</high></span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/vector_types.png"></img> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10"><span><high>Three+ data types</high></span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r print(baselers) ``` ``` ## # A tibble: 10,000 × 20 ## id sex age height weight income ## <dbl> <chr> <dbl> <dbl> <dbl> <dbl> ## 1 1 male 44 174. 113. 6300 ## 2 2 male 65 180. 75.2 10900 ## 3 3 female 31 168. 55.5 5100 ## 4 4 male 27 209 93.8 4200 ## 5 5 male 24 177. NA 4000 ## 6 6 male 63 187. 67.4 11400 ## 7 7 male 71 152. 83.3 12000 ## 8 8 female 41 156. 67.8 7600 ## 9 9 male 43 176. 69.3 8500 ## 10 10 female 31 166. 66.3 6100 ## # … with 9,990 more rows ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10"><span><high>Three+ data types</high></span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # select sex veriable using $ baselers$sex ``` ``` ## [1] "male" "male" "female" "male" ## [5] "male" "male" "male" "female" ## [ reached getOption("max.print") -- omitted 9992 entries ] ``` ```r # select sex veriable using %>% select baselers %>% select(sex) %>% pull() ``` ``` ## [1] "male" "male" "female" "male" ## [5] "male" "male" "male" "female" ## [ reached getOption("max.print") -- omitted 9992 entries ] ``` ```r # Possible, but less pretty... baselers[['sex']] baselers[[2]] ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10"><span><high>Three+ data types</high></span></li> <li class="m11g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r # original sex vector baselers$sex ``` ``` ## [1] "male" "male" "female" "male" "male" ## [ reached getOption("max.print") -- omitted 9995 entries ] ``` ```r as.factor(baselers$sex) ``` ``` ## [1] male male female male male ## [ reached getOption("max.print") -- omitted 9995 entries ] ## Levels: female male ``` ```r as.factor(baselers$weight) ``` ``` ## [1] 113.4 75.2 55.5 93.8 <NA> ## [ reached getOption("max.print") -- omitted 9995 entries ] ## 719 Levels: 37.9 38.3 39.2 39.6 40.3 ... 125.4 ``` ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11"><span><high>Formulae</high></span></li> </ul> ] .pull-right5[ ```r # Run a regression and store result in my_lm my_lm <- lm(formula = income ~ age + height, data = baselers) ``` <br> <img src="https://raw.githubusercontent.com/therbootcamp/Erfurt_2018June/master/_sessions/_image/formula_description.png"></img> ] --- # 11 basic R lessons .pull-left4[ <ul> <li class="m1g"><span>The assignment operator <mono><-</mono></span></li> <li class="m2g"><span><mono><-</mono> creates/changes objects</high></span></li> <li class="m3g"><span>Everythings happens through functions</span></li> <li class="m4g"><span>Functions have (default) arguments</span></li> <li class="m5g"><span>Arguments expect classes</span></li> <li class="m6g"><span>Find help using <mono>?</mono></span></li> <li class="m7g"><span>Chain functions with the pipe</span></li> <li class="m8g"><span>Functions exist in Packages</span></li> <li class="m9g"><span>Data lives in data frames</span></li> <li class="m10g"><span>Three+ data types</span></li> <li class="m11"><span><high>Formulae</high></span></li> </ul> ] .pull-right5[ <high>Add variables</high> using `+` ```r # Include multiple terms with + my_lm <- lm(formula = income ~ age + height, data = baselers) ``` <high>Include all variables</high> using `formula = y ~ .` ```r # Use y ~ . to include ALL variables my_lm <- lm(formula = income ~ ., data = baselers) ``` <high>Subtract variables</high> using `-` ```r # Remove variables with - my_lm <- lm(formula = income ~ . - id, data = baselers) ``` ] --- class: middle, center <h1><a>Interactive</a></h1>