+ - 0:00:00
Notes for current slide
Notes for next slide

1. Intro to R

Analytics Sandbox

K. Bret Staudt Willet | Florida State University

January 11, 2023

1 / 22



Part 1:
Introduction to R

2 / 22

Our Constructivist Approach

3 / 22

Our Constructivist Approach

  1. We'll introduce some concepts
  2. You'll try some code
  3. We'll all discuss together
3 / 22

Our Constructivist Approach

  1. We'll introduce some concepts
  2. You'll try some code
  3. We'll all discuss together

3 / 22



Background
on R and RStudio

4 / 22

Why Learn R?

5 / 22

Why Learn R?

  • It is increasingly used in education
5 / 22

Why Learn R?

  • It is increasingly used in education

  • It is cross-platform, open-source, and freely-available

5 / 22

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

5 / 22

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

5 / 22

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

5 / 22

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

5 / 22

Setting up R

To download R

  • Visit 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 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
6 / 22



Try it Out!

7 / 22

Getting Started with RStudio

Activities:

  1. Running a single RMarkdown chunk
  2. Running another RMarkdown chunk
  3. Rendering an RMarkdown document to a PDF
  4. Creating your first visualization
8 / 22

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)
9 / 22

Try it Out!

10 / 22

Try it Out!

What do you think this code will do?

starwars %>%
filter(sex == "female") %>%
select(name, hair_color, skin_color, homeworld)
11 / 22

Try it Out!

Let's see!

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
12 / 22

Try it Out!

What do you think this code will do?

starwars %>%
filter(sex %in% c("male", "none"),
height <= 150) %>%
select(name, sex, height, mass, homeworld) %>%
arrange(desc(height))
13 / 22

Try it Out!

Let's see!

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
14 / 22

Try it Out!

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
15 / 22

Try it Out!

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
16 / 22

Exploring further

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",…
17 / 22

Visualizing data

starwars %>%
ggplot() +
geom_point(aes(x = mass, y = height, color = gender),
alpha = 0.5
) +
theme_bw()

18 / 22



Try it out!

Hop over to Workbook 1

19 / 22



Appendix:
Helpful Resources
and Troubleshooting

20 / 22

Resources

Beginners:

Intermediates:

Experts:

21 / 22

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
  • General strategies on learning more: Chapter 17 of Data Science in Education Using R
22 / 22



Part 1:
Introduction to R

2 / 22
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow