data <- read_excel("../00_data/MyData.xlsx")
Make two bar charts here - one before ordering another after
# Transform data: calculate average male athletes by state
State_cd_by_ef_male_count <- data %>%
group_by(state_cd) %>%
summarise(
avg_ef_male_count = mean(ef_male_count,na.rm = TRUE)
)
State_cd_by_ef_male_count
## # A tibble: 54 × 2
## state_cd avg_ef_male_count
## <chr> <dbl>
## 1 AK 2009.
## 2 AL 1893.
## 3 AR 1402.
## 4 AZ 3724.
## 5 CA 3201.
## 6 CO 2656.
## 7 CT 1999.
## 8 DC 1983.
## 9 DE 2294.
## 10 FL 3040.
## # ℹ 44 more rows
# Plot
State_cd_by_ef_male_count %>%
ggplot(aes(x = 'ef_male_count' , y = 'state_cd')) +
geom_point()
State_cd_by_ef_male_count %>%
ggplot(aes(x = avg_ef_male_count, y = fct_reorder(.f = state_cd, .x = avg_ef_male_count))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean male count in each state")
State_cd_by_ef_male_count %>%
ggplot(aes(x = avg_ef_male_count,
y = fct_reorder(.f = state_cd, .x = avg_ef_male_count) %>%
fct_relevel("Don't know"))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean male count in each state")
## Warning: 1 unknown level in `f`: Don't know
## 1 unknown level in `f`: Don't know
Show examples of three functions:
data %>% distinct(sports)
## # A tibble: 38 × 1
## sports
## <chr>
## 1 Baseball
## 2 Basketball
## 3 All Track Combined
## 4 Football
## 5 Golf
## 6 Soccer
## 7 Softball
## 8 Tennis
## 9 Volleyball
## 10 Bowling
## # ℹ 28 more rows
# Recode
data %>%
# Rename levels
mutate(sports_rev = fct_recode(sports,"Volleyball" = "V-ball")) %>%
select(sports, sports_rev) %>%
filter(sports == "v-ball")
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `sports_rev = fct_recode(sports, Volleyball = "V-ball")`.
## Caused by warning:
## ! Unknown levels in `f`: V-ball
## # A tibble: 0 × 2
## # ℹ 2 variables: sports <chr>, sports_rev <fct>
# Collapse multiple levels into one
data %>%
mutate(sports_col = fct_collapse(sports, "Minority" = c("volleyball","Other"))) %>%
select(sports, sports_col) %>%
filter(sports != "baseball")
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `sports_col = fct_collapse(sports, Minority = c("volleyball",
## "Other"))`.
## Caused by warning:
## ! Unknown levels in `f`: volleyball, Other
## # A tibble: 132,327 × 2
## sports sports_col
## <chr> <fct>
## 1 Baseball Baseball
## 2 Basketball Basketball
## 3 All Track Combined All Track Combined
## 4 Football Football
## 5 Golf Golf
## 6 Soccer Soccer
## 7 Softball Softball
## 8 Tennis Tennis
## 9 Volleyball Volleyball
## 10 Bowling Bowling
## # ℹ 132,317 more rows
# Lump small levels into other levels
data %>% count(sports)
## # A tibble: 38 × 2
## sports n
## <chr> <int>
## 1 All Track Combined 4870
## 2 Archery 1557
## 3 Badminton 1554
## 4 Baseball 8644
## 5 Basketball 10000
## 6 Beach Volleyball 1988
## 7 Bowling 2176
## 8 Diving 1530
## 9 Equestrian 1799
## 10 Fencing 1687
## # ℹ 28 more rows
data %>% mutate(sports_lump = fct_lump(sports)) %>% distinct(sports_lump)
## # A tibble: 38 × 1
## sports_lump
## <fct>
## 1 Baseball
## 2 Basketball
## 3 All Track Combined
## 4 Football
## 5 Golf
## 6 Soccer
## 7 Softball
## 8 Tennis
## 9 Volleyball
## 10 Bowling
## # ℹ 28 more rows
No need to do anything here.