Do not change anything in the following chunk

You will be working on olympic_gymnasts dataset. Do not change the code below:

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)) %>%             # only keep athletes with known age
  filter(sport == "Gymnastics") %>%   # keep only gymnasts
  mutate(
    medalist = case_when(             # add column for success in medaling
      is.na(medal) ~ FALSE,           # NA values go to FALSE
      !is.na(medal) ~ TRUE            # non-NA values (Gold, Silver, Bronze) go to TRUE
    )
  )

More information about the dataset can be found at

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

Question 1: Create a subset dataset with the following columns only: name, sex, age, team, year and medalist. Call it df.

df<- olympic_gymnasts|>
  select(name, sex, age, team, year, medalist)
str(df)
## tibble [25,528 × 6] (S3: tbl_df/tbl/data.frame)
##  $ name    : chr [1:25528] "Paavo Johannes Aaltonen" "Paavo Johannes Aaltonen" "Paavo Johannes Aaltonen" "Paavo Johannes Aaltonen" ...
##  $ sex     : chr [1:25528] "M" "M" "M" "M" ...
##  $ age     : num [1:25528] 28 28 28 28 28 28 28 28 32 32 ...
##  $ team    : chr [1:25528] "Finland" "Finland" "Finland" "Finland" ...
##  $ year    : num [1:25528] 1948 1948 1948 1948 1948 ...
##  $ medalist: logi [1:25528] TRUE TRUE FALSE TRUE FALSE FALSE ...

Question 2: From df create df2 that only have year of 2008 2012, and 2016

target <- c(2008, 2012, 2016)
df2 <- df %>% filter(year %in% target)

Question 3 Group by these three years (2008,2012, and 2016) and summarize the mean of the age in each group.

df3 <- df2 %>% group_by(year) %>% summarise(averageage = mean(age))

Question 4 Use olympic_gymnasts dataset, group by year, and find the mean of the age for each year, call this dataset oly_year. (optional after creating the dataset, find the minimum average age)

oly_year <- olympic_gymnasts %>% group_by(year) %>% summarize(averageage = mean(age))

Question 5 This question is open ended. Create a question that requires you to use at least two verbs. Create a code that answers your question. Then below the chunk, reflect on your question choice and coding procedure

#Open-ended question: 
#Which countries have gold-winning Olympic Gymnasts in the year 2012?

# Your R code here
openended <- olympic_gymnasts %>% filter(medal == "Gold")
openended <- openended %>% select(noc,year,medal) %>% filter(year == "2012") %>% group_by(noc) %>% distinct()

print(openended)
## # A tibble: 9 × 3
## # Groups:   noc [9]
##   noc    year medal
##   <chr> <dbl> <chr>
## 1 HUN    2012 Gold 
## 2 CHN    2012 Gold 
## 3 USA    2012 Gold 
## 4 ROU    2012 Gold 
## 5 RUS    2012 Gold 
## 6 JPN    2012 Gold 
## 7 KOR    2012 Gold 
## 8 BRA    2012 Gold 
## 9 NED    2012 Gold

Discussion: Enter your discussion of results here.

I started with the Olympic gymnast dataset. I kept the data for countries that earned medals, countries that won Gold medals specifically, in the year 2012, I then used the group_by function (to organize by Country) and the distinct function (which removes duplicates in my table, since some countries won more than one Gold medal) to create a table that shows all countries that won Gold medals for gymnastics in 2012: Hungary, China, United States, Romania, Russia, Japan, Korea, Brasil, and the Netherlands.