class: left, bottom, title-slide .title[ # 1. Intro to R ] .subtitle[ ## Analytics Sandbox ] .author[ ### K. Bret Staudt Willet | Florida State University ] .date[ ### January 11, 2023 ] --- class: inverse, center, middle #
<br><br> **Part 1:** <br> Introduction to R --- #
Our Constructivist Approach -- 1. We'll introduce some concepts 1. You'll try some code 1. We'll all discuss together -- <img src="img/tech_support_cheat_sheet.png" width="360px" style="display: block; margin: auto;" /> <div class="caption"> <p><a href="https://xkcd.com/627/" target="_blank">'Tech Support Cheat Sheet' from XKCD</a></p> </div> --- class: inverse, center, middle #
<br><br> Background <br> on R and RStudio --- #
Why Learn R? -- - It is increasingly used in education -- - It is cross-platform, open-source, and freely-available -- - It is a programming language and quite flexible -- - It is capable of carrying out basic and complex statistical analyses -- - It is able to work with data small (*n* = 10) and large (*n* = 1,000,000+) efficiently -- - There is a great, inclusive community of users and developers --- #
Setting up R ### To download R - Visit [**cran.r-project.org**](https://cran.r-project.org/) to download R - Find your operating system (Mac, Windows, or Linux) - Download the 'latest release' on the page for your operating system and download and install the application ### To download RStudio - Visit [**rstudio.com**](https://rstudio.com/products/rstudio/download/) to download RStudio - Find your operating system (Mac, Windows, or Linux) - Download the 'latest release' on the page for your operating system and download and install the application --- class: inverse, center, middle #
<br><br> Try it Out! --- #
Getting Started with RStudio Activities: 1. Running a single RMarkdown chunk 1. Running another RMarkdown chunk 1. Rendering an RMarkdown document to a PDF 1. Creating your first visualization --- #
RMarkdown - RMarkdown is a data analysis "notebook" that combines text with code and output - It is a great file type to use when beginning to use R and to create reproducible analyses - It is fun to use because you can generate different types of output (Word, PDF, and even web-based) --- class: inverse, center, middle #
Try it Out! --- #
Try it Out! What do you think this code will do? ```r starwars %>% filter(sex == "female") %>% select(name, hair_color, skin_color, homeworld) ``` --- #
Try it Out! Let's see! ```r starwars %>% filter(sex == "female") %>% select(name, hair_color, skin_color, homeworld) ``` ``` ## # A tibble: 16 × 4 ## name hair_color skin_color homeworld ## <chr> <chr> <chr> <chr> ## 1 Leia Organa brown light Alderaan ## 2 Beru Whitesun lars brown light Tatooine ## 3 Mon Mothma auburn fair Chandrila ## 4 Shmi Skywalker black fair Tatooine ## 5 Ayla Secura none blue Ryloth ## 6 Adi Gallia none dark Coruscant ## 7 Cordé brown light Naboo ## 8 Luminara Unduli black yellow Mirial ## 9 Barriss Offee black yellow Mirial ## 10 Dormé brown light Naboo ## 11 Zam Wesell blonde fair, green, yellow Zolan ## 12 Taun We none grey Kamino ## 13 Jocasta Nu white fair Coruscant ## 14 Shaak Ti none red, blue, white Shili ## 15 Rey brown light <NA> ## 16 Padmé Amidala brown light Naboo ``` --- #
Try it Out! What do you think this code will do? ```r starwars %>% filter(sex %in% c("male", "none"), height <= 150) %>% select(name, sex, height, mass, homeworld) %>% arrange(desc(height)) ``` --- #
Try it Out! Let's see! ```r starwars %>% filter(sex %in% c("male", "none"), height <= 150) %>% arrange(height) %>% select(name, sex, height, mass, homeworld) ``` ``` ## # A tibble: 10 × 5 ## name sex height mass homeworld ## <chr> <chr> <int> <dbl> <chr> ## 1 Yoda male 66 17 <NA> ## 2 Ratts Tyerell male 79 15 Aleen Minor ## 3 Wicket Systri Warrick male 88 20 Endor ## 4 Dud Bolt male 94 45 Vulpter ## 5 R2-D2 none 96 32 Naboo ## 6 R4-P17 none 96 NA <NA> ## 7 R5-D4 none 97 32 Tatooine ## 8 Sebulba male 112 40 Malastare ## 9 Gasgano male 122 NA Troiken ## 10 Watto male 137 NA Toydaria ``` --- #
Try it Out! ```r starwars %>% unnest(starships) %>% select(name, gender, starships) %>% head(10) ``` ``` ## # A tibble: 10 × 3 ## name gender starships ## <chr> <chr> <chr> ## 1 Luke Skywalker masculine X-wing ## 2 Luke Skywalker masculine Imperial shuttle ## 3 Darth Vader masculine TIE Advanced x1 ## 4 Biggs Darklighter masculine X-wing ## 5 Obi-Wan Kenobi masculine Jedi starfighter ## 6 Obi-Wan Kenobi masculine Trade Federation cruiser ## 7 Obi-Wan Kenobi masculine Naboo star skiff ## 8 Obi-Wan Kenobi masculine Jedi Interceptor ## 9 Obi-Wan Kenobi masculine Belbullab-22 starfighter ## 10 Anakin Skywalker masculine Trade Federation cruiser ``` --- #
Try it Out! ```r starwars %>% unnest(starships) %>% mutate(vehicles = strsplit(starships, ",")) %>% unnest(starships) %>% select(name, gender, starships) %>% group_by(gender) %>% count() ``` ``` ## # A tibble: 3 × 2 ## # Groups: gender [3] ## gender n ## <chr> <int> ## 1 feminine 3 ## 2 masculine 27 ## 3 <NA> 1 ``` --- #
Exploring further ```r glimpse(starwars) ``` ``` ## Rows: 87 ## Columns: 14 ## $ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or… ## $ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2… ## $ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.… ## $ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N… ## $ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "… ## $ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",… ## $ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, … ## $ sex <chr> "male", "none", "none", "male", "female", "male", "female",… ## $ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini… ## $ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T… ## $ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma… ## $ films <list> <"The Empire Strikes Back", "Revenge of the Sith", "Return… ## $ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp… ## $ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",… ``` --- #
Visualizing data ```r starwars %>% ggplot() + geom_point(aes(x = mass, y = height, color = gender), alpha = 0.5 ) + theme_bw() ``` <!-- --> --- class: inverse, center, middle #
<br><br> Try it out! Hop over to [**Workbook 1**](workbooks/workbook1.Rmd) --- class: inverse, center, middle #
<br><br> Appendix: <br> Helpful Resources <br> and Troubleshooting --- # Resources **Beginners:** - [RStudio Beginners' Guide](https://education.rstudio.com/learn/beginner/) - Book: [*Data Science in Education Using R*](https://datascienceineducation.com) - See [Chapter 12](https://datascienceineducation.com/c12.html) - Walkthrough 6: Exploring Relationships Using Social Network Analysis With Social Media Data - [Physical copy of DSIEUR](https://www.routledge.com/Data-Science-in-Education-Using-R/Estrellado-Freer-Mostipak-Rosenberg-Velasquez/p/book/9780367422257) - [Even more resources from DSIEUR](https://datascienceineducation.com/c18.html) **Intermediates:** - [RStudio Intermediates' Guide](https://education.rstudio.com/learn/intermediate/) - [{tidytags} package notes](https://docs.ropensci.org/tidytags/index.html) - Book: [*R for Data Science*](http://r4ds.had.co.nz/) **Experts:** - [RStudio Experts' Guide](https://education.rstudio.com/learn/expert/) - Book: [*Learning Statistics with R*](https://learningstatisticswithr.com/) - [*Data Science in Education Using R*](https://datascienceineducation.com) - See [Chapter 20.3 Appendix C](https://datascienceineducation.com/c20.html#c20c) - Social Network Influence and Selection Models - SNA resources: [Dr. Ken Frank's website](https://sites.google.com/msu.edu/kenfrank/social-network-resources) --- # Troubleshooting - Try to find out what the specific problem is - Identify what is *not* causing the problem - "Unplug and plug it back in" - restart R; close and reopen R - Seek out workshops and other learning opportunities - Reach out to others! Sharing what is causing an issue can often help to clarify the problem - [RStudio Community forum](https://community.rstudio.com/) (highly recommended!) - Twitter hashtag: [#RStats](https://twitter.com/search?q=%23RStats&src=typeahead_click&f=live) - [Contact Bret!](https://bretsw.com) - General strategies on learning more: [Chapter 17 of *Data Science in Education Using R*](https://datascienceineducation.com/c17.html)