class: center, middle, inverse, title-slide # Intro to R ### Applied Machine Learning with R
The R Bootcamp @ AMLD
### January 2020 --- 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 | January 2020 </font> </a> </span> </div> --- # Why R? .pull-left3[ R steadily **grows in popularity**. Today, R is one of the **most popular languages for data science** and overall. In terms of the number of data science jobs, **R beats SAS and Matlab**, and is on par with Python. <p style="font-size:10px;float:bottom"> Image source: https://i0.wp.com/r4stats.com/ <p> ] .pull-right6[ <p align="center"> <img src="https://i0.wp.com/r4stats.com/wp-content/uploads/2017/02/Fig-1a-IndeedJobs-2017.png" height="450"> </p> ] --- # R is great for ML ...because of high-performance R packages (extensions) downloaded and used millions of times. <p align="center"><img src="image/ml_package_dl.png" width="620"></p> --- # R is so popular because There are many good reasons to prefer R over superficially more user friendly software such as **Excel** or **SPSS** or more complex programming languages like **C++** or **Python**. .pull-left45[ ### Pro 1. **It's free** 2. Relatively **easy** 3. **Extensibility** ([CRAN](https://cran.r-project.org/), packages) 4. **User base** (e.g., [stackoverflow](https://stackoverflow.com/)) 5. [**Tidyverse**](https://www.tidyverse.org/) (`dplyr`, `ggplot`, etc.) 6. [**RStudio**](https://www.rstudio.com/) 7. **Productivity** options: [Latex](https://www.latex-project.org/), [Markdown](https://daringfireball.net/projects/markdown/), [GitHub](https://github.com/) ] .pull-right45[ ### Con (?) It's slow, but... [Tidyverse](https://www.tidyverse.org/) [Rcpp](http://www.rcpp.org/), [BH](https://cran.r-project.org/web/packages/BH/index.html): Links R to C++ and high-performance C++ libraries<br> [rPython](http://rpython.r-forge.r-project.org/): Links R to Python<br> [RHadoop](https://github.com/RevolutionAnalytics/RHadoop/wiki): Links R to Hadoop for big data applications.<br> ] --- <div class="center_text"> <span> 10 basic R lessons </span> </div> --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7"><span>Functions exist in Packages</span></li> <li class="m8"><span>Data lives in data frames</span></li> <li class="m9"><span>Three+ data types</span></li> <li class="m10"><span>Formulae</span></li> </ul> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ <br> <p align="center"> <img src="image/assignment.png"> </p> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ <br><br> <p align="center"><img src="image/input_output.png" height="300"></p> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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" ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r ?mean ``` <p align="center"><img src="image/help_mean.png" width="500"></p> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r ?mean ``` <p align="center"><img src="image/help_usage.png" width="500"></p> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7"><span><high>Functions exist in Packages</high></span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ <p align="center"><img src="image/packages.png" height="400"></p> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7"><span><high>Functions exist in Packages</high></span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><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> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8"><span><high>Data lives in data frames</high></span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right55[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/main_objects.png"></img> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9"><span><high>Three+ data types</high></span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/vector_types.png"></img> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9"><span><high>Three+ data types</high></span></li> <li class="m10g"><span>Formulae</span></li> </ul> ] .pull-right5[ ```r print(baselers) ``` ``` ## # A tibble: 10,000 x 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 fema… 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 fema… 41 156. 67.8 7600 ## 9 9 male 43 176. 69.3 8500 ## 10 10 fema… 31 166. 66.3 6100 ## # … with 9,990 more rows, and 14 more variables ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9"><span><high>Three+ data types</high></span></li> <li class="m10g"><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]] ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9"><span><high>Three+ data types</high></span></li> <li class="m10g"><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 ``` ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10"><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> ] --- # 10 basic R lessons .pull-left4[ <b>Syntax</b> <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> </ul> <b>Analysis</b> <ul> <li class="m7g"><span>Functions exist in Packages</span></li> <li class="m8g"><span>Data lives in data frames</span></li> <li class="m9g"><span>Three+ data types</span></li> <li class="m10"><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>