Import your data

data <- read_excel("../00_data/myData.xlsx")

Chapter 15

Create a factor

set.seed(12345)
data_small <- data %>%
    select(egg_group_1, attack) %>%
    slice(1:20)
data_small %>% count(egg_group_1)
## # A tibble: 4 × 2
##   egg_group_1     n
##   <chr>       <int>
## 1 bug             6
## 2 flying          3
## 3 ground          2
## 4 monster         9
egg_group_1_levels <- c("monster", "bug", "flying", "ground")

data_rev <- data_small %>%
    mutate(egg_group_1 = egg_group_1 %>% factor(levels = egg_group_1_levels))

Modify factor order

Make two bar charts here - one before ordering another after

data_summary <- data_small %>%
  group_by(egg_group_1) %>%
  summarise(
    attack = mean(attack, na.rm = TRUE))


ggplot(data_summary, aes(attack, egg_group_1)) + geom_point()

ggplot(data_summary, aes(attack, fct_reorder(egg_group_1, attack))) + geom_point()

Modify factor levels

Show examples of three functions:

  • fct_recode
data_small %>%
    mutate(egg_group_1 = fct_recode(egg_group_1,
                                    "ground_egg" = "ground",
                                    "monster_egg" = "monster",
                                    "flying_egg" = "flying",
                                    "bug_egg" = "bug")) %>%
    count(egg_group_1)
## # A tibble: 4 × 2
##   egg_group_1     n
##   <fct>       <int>
## 1 bug_egg         6
## 2 flying_egg      3
## 3 ground_egg      2
## 4 monster_egg     9
  • fct_collapse
data_small %>%
    mutate(egg_group_1 = fct_collapse(egg_group_1,
                                      "ground_egg" = "ground",
                                    other        = c("monster", "flying", "bug"))) %>%
    count(egg_group_1)
## # A tibble: 2 × 2
##   egg_group_1     n
##   <fct>       <int>
## 1 other          18
## 2 ground_egg      2
  • fct_lump
data_small %>%
    mutate(egg_group_1 = fct_lump(egg_group_1)) %>%
    count(egg_group_1)
## # A tibble: 3 × 2
##   egg_group_1     n
##   <fct>       <int>
## 1 bug             6
## 2 monster         9
## 3 Other           5

Chapter 16

No need to do anything here.