Import your data

data <- readxl::read_xlsx("../00_data/Pokemon Data.xlsx")

Chapter 15

Create a factor

type_1small <- data %>%
    select(type_1) %>%
    sample_n(3)

type_1small <- c("electric", "ground", "ice")

data_rev <- data %>%
    mutate(type_1 = type_1 %>% factor(levels = type_1small))

Modify factor order

Make two bar charts here - one before ordering another after

data_summary <- data %>%
    group_by(type_1) %>%
    summarise(
        attack = mean(attack, na.rm = TRUE)
    )
data_summary
## # A tibble: 18 × 2
##    type_1   attack
##    <chr>     <dbl>
##  1 bug        72.5
##  2 dark       84.7
##  3 dragon    109. 
##  4 electric   68.1
##  5 fairy      61.2
##  6 fighting   99.1
##  7 fire       84.3
##  8 flying     78.8
##  9 ghost      76.3
## 10 grass      75.2
## 11 ground     96.2
## 12 ice        73.1
## 13 normal     75.9
## 14 poison     73.5
## 15 psychic    72.5
## 16 rock       90.0
## 17 steel      93.1
## 18 water      74.7
ggplot(data_summary, aes(attack, type_1)) + geom_point()

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

Modify factor levels

Show examples of three functions:

data %>%
  mutate(type_1 = factor(type_1)) %>%
  mutate(type_1 = fct_recode(type_1,
    "Flame" = "fire",
    "Aqua" = "water",
    "Nature" = "grass"
  )) %>%
  count(type_1, sort = TRUE)
## # A tibble: 18 × 2
##    type_1       n
##    <fct>    <int>
##  1 Aqua       126
##  2 normal     111
##  3 Nature      84
##  4 bug         79
##  5 rock        65
##  6 psychic     64
##  7 electric    61
##  8 Flame       59
##  9 ghost       40
## 10 dragon      39
## 11 dark        37
## 12 ground      36
## 13 poison      35
## 14 fighting    31
## 15 steel       30
## 16 ice         29
## 17 fairy       19
## 18 flying       4
data_summary %>%
    mutate(type_1 = fct_lump(type_1, n = 5)) %>%
    count(type_1, sort = TRUE) 
## # A tibble: 18 × 2
##    type_1       n
##    <fct>    <int>
##  1 bug          1
##  2 dark         1
##  3 dragon       1
##  4 electric     1
##  5 fairy        1
##  6 fighting     1
##  7 fire         1
##  8 flying       1
##  9 ghost        1
## 10 grass        1
## 11 ground       1
## 12 ice          1
## 13 normal       1
## 14 poison       1
## 15 psychic      1
## 16 rock         1
## 17 steel        1
## 18 water        1
data %>% 
    mutate(type_1 = fct_collapse(type_1,
        "Main Types" = c("fire", "water", "grass"),
        "Other Types" = c("electric", "ice", "rock")
                                 
    )) %>%
    count(type_1)
## # A tibble: 14 × 2
##    type_1          n
##    <fct>       <int>
##  1 bug            79
##  2 dark           37
##  3 dragon         39
##  4 Other Types   155
##  5 fairy          19
##  6 fighting       31
##  7 Main Types    269
##  8 flying          4
##  9 ghost          40
## 10 ground         36
## 11 normal        111
## 12 poison         35
## 13 psychic        64
## 14 steel          30
  • fct_recode
  • fct_collapse
  • fct_lump

Chapter 16

No need to do anything here.