dog <- read_excel("../01_module4/data/myData.xlsx") %>% filter(!Length %>% str_detect("Hounds"))
dog %>% count(Length)
## # A tibble: 3 × 2
## Length n
## <chr> <int>
## 1 Long 29
## 2 Medium 79
## 3 Short 86
length_hair <- c("Short", "Medium", "Long")
dog_rev <- dog %>%
mutate(Length = Length %>% factor(levels = length_hair))
Make two bar charts here - one before ordering another after
dog_summary <- dog_rev %>%
group_by(Length) %>%
summarise(
shed = mean(`Shedding Level`, na.rm = TRUE),
)
dog_summary
## # A tibble: 3 × 2
## Length shed
## <fct> <dbl>
## 1 Short 2.73
## 2 Medium 2.68
## 3 Long 2
ggplot(dog_summary, aes(shed, Length)) + geom_point()
ggplot(dog_summary, aes(shed, fct_reorder(Length, shed))) + geom_point()
Show examples of three functions:
dog_rev %>%
mutate(Length = fct_recode(Length,
"Sm" = "Short",
"Med" = "Medium",
"Lg" = "Long")) %>%
count(Length)
## # A tibble: 3 × 2
## Length n
## <fct> <int>
## 1 Sm 86
## 2 Med 79
## 3 Lg 29
dog_rev %>%
mutate(Length = fct_collapse(Length,
Sm = "Short",
Other = c("Medium", "Long"))) %>%
count(Length)
## # A tibble: 2 × 2
## Length n
## <fct> <int>
## 1 Sm 86
## 2 Other 108
dog %>%
mutate(Length_rev = Length %>% fct_lump_n(1)) %>%
count(Length_rev)
## # A tibble: 2 × 2
## Length_rev n
## <fct> <int>
## 1 Short 86
## 2 Other 108
No need to do anything here.