This homework has two parts. Part 1 uses base R to inspect a dataframe. Part 2 uses dplyr to wrangle a different dataset.


Part 1 — Student Survey (dataframe basics)

Download StudentSurvey.csv from the Datasets folder on Blackboard. Save it next to this Rmd and set your working directory.

# Load the file
survey <- read.csv("StudentSurvey.csv")

control + alt + i

# Q1. Check the head of the dataset
head(survey)
##        Year Sex Smoke   Award HigherSAT Exercise TV Height Weight Siblings
## 1    Senior   M    No Olympic      Math       10  1     71    180        4
## 2 Sophomore   F   Yes Academy      Math        4  7     66    120        2
## 3 FirstYear   M    No   Nobel      Math       14  5     72    208        2
## 4    Junior   M    No   Nobel      Math        3  1     63    110        1
## 5 Sophomore   F    No   Nobel    Verbal        3  3     65    150        1
## 6 Sophomore   F    No   Nobel    Verbal        5  4     65    114        2
##   BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings
## 1          4       540     670 1210 3.13    54         0
## 2          2       520     630 1150 2.50    66         3
## 3          1       550     560 1110 2.55   130         0
## 4          1       490     630 1120 3.10    78         0
## 5          1       720     450 1170 2.70    40         6
## 6          2       600     550 1150 3.20    80         4
# Q2. Check the dimensions
dim(survey)
## [1] 362  17
# Q3. Create a table of students' sex and HigherSAT
table(survey$Sex, survey$HigherSAT)
##    
##         Math Verbal
##   F   4   81     84
##   M   3  124     66
# Q4. Display summary statistics for VerbalSAT
summary(survey$VerbalSAT)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   390.0   550.0   600.0   594.2   640.0   800.0
# Q5. Find the average GPA of students
mean(survey$GPA, na.rm = TRUE)
## [1] 3.157942
# Q6. Create a new dataframe called column_df that contains students' weight
#     and number of hours they exercise.
column_df <- survey |>
  select(Exercise, Weight)
head(column_df)
##   Exercise Weight
## 1       10    180
## 2        4    120
## 3       14    208
## 4        3    110
## 5        3    150
## 6        5    114
# Q7. Access the fourth element in the first column of the StudentSurvey dataset.
survey$Year[4]
## [1] "Junior"

Part 2 — Olympic Gymnasts (dplyr)

Don’t change this chunk — it loads and filters the dataset.

olympics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-07-27/olympics.csv')

olympic_gymnasts <- olympics |>
  filter(!is.na(age)) |>
  filter(sport == "Gymnastics") |>
  mutate(
    medalist = case_when(
      is.na(medal) ~ FALSE,
      !is.na(medal) ~ TRUE
    )
  )

More info on the data: https://github.com/rfordatascience/tidytuesday/blob/master/data/2021/2021-07-27/readme.md

# Q8. Create a subset dataframe with these columns only: name, sex, age, team, year, medalist.
#     Call it df.
df <- olympic_gymnasts |>
  select(name, sex, age, team, year, medalist)
head(df)
## # A tibble: 6 × 6
##   name                    sex     age team     year medalist
##   <chr>                   <chr> <dbl> <chr>   <dbl> <lgl>   
## 1 Paavo Johannes Aaltonen M        28 Finland  1948 TRUE    
## 2 Paavo Johannes Aaltonen M        28 Finland  1948 TRUE    
## 3 Paavo Johannes Aaltonen M        28 Finland  1948 FALSE   
## 4 Paavo Johannes Aaltonen M        28 Finland  1948 TRUE    
## 5 Paavo Johannes Aaltonen M        28 Finland  1948 FALSE   
## 6 Paavo Johannes Aaltonen M        28 Finland  1948 FALSE
# Q9. From df, create df2 that only has the years 2008, 2012, and 2016.
df2 <- df |>
  filter(year %in% c(2008, 2012, 2016)) |>
  arrange(desc(year))

# Q10. Group by those three years and summarize the mean age in each group.
df2 |>
  group_by(year) |>
  summarize(mean_age = mean(age)) 
## # A tibble: 3 × 2
##    year mean_age
##   <dbl>    <dbl>
## 1  2008     21.6
## 2  2012     21.9
## 3  2016     22.2
# Q11. Using the full olympic_gymnasts dataset, group by year and find the mean age
#      for each year. Call this oly_year.
#      (Bonus: find the minimum average age across years.)
oly_year <- olympic_gymnasts |>
  group_by(year) |>
  summarise(mean_age = mean(age))
oly_year
## # A tibble: 29 × 2
##     year mean_age
##    <dbl>    <dbl>
##  1  1896     24.3
##  2  1900     22.2
##  3  1904     25.1
##  4  1906     24.7
##  5  1908     23.2
##  6  1912     24.2
##  7  1920     26.7
##  8  1924     27.6
##  9  1928     25.6
## 10  1932     23.9
## # ℹ 19 more rows
# Q12. Open-ended: come up with a question that requires at least TWO dplyr verbs.
#      Write the question, then the code that answers it. 
round(mean(olympic_gymnasts$age), 2)
## [1] 22.73
#   Which team has the highest number of gymnasts who medal after the age of 23?
oly_team <- olympic_gymnasts |>
  filter(age > 23 & medalist == TRUE) |>
  group_by(team) |>
  count() |>
  arrange(desc(n)) |>
  head(1)
oly_team
## # A tibble: 1 × 2
## # Groups:   team [1]
##   team      n
##   <chr> <int>
## 1 Japan   121
#      Below the chunk, briefly explain why you chose this question.

I was interested in Olympic gymnasts who have success later in their careers. The average age is less than 23 years old. I was interested in finding out the Olympic team with the greatest number of gymnasts who medal after the age of 23.

Your question and reflection: