Import your data

myData <-read_csv ("../00_data/myData.csv")
## Rows: 27 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): film, film_rating
## dbl  (2): number, run_time
## date (1): release_date
## 
## ℹ 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

myData%>% count(film_rating)
## # A tibble: 4 × 2
##   film_rating     n
##   <chr>       <int>
## 1 G              13
## 2 N/A             3
## 3 Not Rated       1
## 4 PG             10
film_levels <- c("G","PG","N/A")

myData_rev<- myData%>%
    mutate(film_rating= film_rating%>% factor(levels = film_levels))

Modify factor order

Make two bar charts here - one before ordering another after

myData_summary <- myData %>%
  group_by(film_rating) %>%
  summarise(
    run_time = mean(run_time, na.rm = TRUE))
    

ggplot(myData_summary, aes(run_time, fct_reorder(film_rating, run_time))) + geom_point()

Modify factor levels

Show examples of three functions:

  • fct_recode
myData%>%
    mutate(film_rating= fct_recode(film_rating, 
                                   "GeneralAudience" = "G",
                                   "ParentalGuidance" = "PG",
                                   "NotApplicable" = "N/A",
                                   "NoRating" = "Not Rated")) %>%
    count(film_rating)
## # A tibble: 4 × 2
##   film_rating          n
##   <fct>            <int>
## 1 GeneralAudience     13
## 2 NotApplicable        3
## 3 NoRating             1
## 4 ParentalGuidance    10
  • fct_collapse
myData%>%
    mutate(film_rating = fct_collapse(film_rating,
                                      GeneralAudience = "G",
                                      Other = c("PG","N/A","Not Rated")))%>%
    count(film_rating)
## # A tibble: 2 × 2
##   film_rating         n
##   <fct>           <int>
## 1 GeneralAudience    13
## 2 Other              14
  • fct_lump
myData%>%
    mutate(film_rating = fct_lump(film_rating))%>%
    count(film_rating)
## # A tibble: 3 × 2
##   film_rating     n
##   <fct>       <int>
## 1 G              13
## 2 PG             10
## 3 Other           4

Chapter 16

No need to do anything here.