Import your data

data <- read_csv("../00_data/survivalists.csv")
## Rows: 94 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): name, gender, city, state, country, reason_tapped_out, reason_cate...
## dbl  (5): season, age, result, days_lasted, day_linked_up
## lgl  (1): medically_evacuated
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Chapter 15

Create a factor

Transform data: calculate average tv hours by religion

 data_by_season <- data %>%
    group_by(season) %>%
    summarise(
        avg_season = mean(season, na.rm = TRUE)
    )

data_by_season
## # A tibble: 9 × 2
##   season avg_season
##    <dbl>      <dbl>
## 1      1          1
## 2      2          2
## 3      3          3
## 4      4          4
## 5      5          5
## 6      6          6
## 7      7          7
## 8      8          8
## 9      9          9
# Plot
data %>%
    
    ggplot(aes((x = season = mean(days_lasted, na.rm = TRUE)), y = days_lasted)) +
   geom_point()

Modify factor order

Make two bar charts here - one before ordering another after

data %>%
    
    ggplot(aes(x = days_lasted, y = fct_reorder(.f = reason_tapped_out, .x = days_lasted))) +
    geom_point() +
    
    # Labeling
    labs(y = NULL, x = "Mean days lasted")

Modify factor levels

Show examples of three functions:

  • fct_recode
  • fct_collapse
  • fct_lump
data %>%
    
    transmute("days_lasted") %>%
    
    ggplot(aes(x = "season", y = 100)) 

Chapter 16

No need to do anything here.