Challenge 8 Instructions

true
true
true
Joining Data
Author

Linda Humphrey

Published

April 27, 2022

library(tidyverse)
library(viridis)
library(patchwork)
library(hrbrthemes)
library(fmsb)
library(colormap)


knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Challenge Overview

Overview of SNL Dataset

Read in data

Reading 3 differnt datasets of snl csv files

#Load dataset

snl_seasons = read_csv("~/Desktop/601_Spring_2023/posts/_data/snl_seasons.csv", show_col_types = FALSE)

snl_casts = read_csv("~/Desktop/601_Spring_2023/posts/_data/snl_casts.csv", show_col_types = FALSE)

snl_actors = read_csv("~/Desktop/601_Spring_2023/posts/_data/snl_actors.csv", show_col_types = FALSE)

head(snl_seasons)
# A tibble: 6 × 5
    sid  year first_epid last_epid n_episodes
  <dbl> <dbl>      <dbl>     <dbl>      <dbl>
1     1  1975   19751011  19760731         24
2     2  1976   19760918  19770521         22
3     3  1977   19770924  19780520         20
4     4  1978   19781007  19790526         20
5     5  1979   19791013  19800524         20
6     6  1980   19801115  19810411         13
head(snl_casts)
# A tibble: 6 × 8
  aid                sid featured first_epid last_epid update_anchor n_episodes
  <chr>            <dbl> <lgl>         <dbl>     <dbl> <lgl>              <dbl>
1 A. Whitney Brown    11 TRUE       19860222        NA FALSE                  8
2 A. Whitney Brown    12 TRUE             NA        NA FALSE                 20
3 A. Whitney Brown    13 TRUE             NA        NA FALSE                 13
4 A. Whitney Brown    14 TRUE             NA        NA FALSE                 20
5 A. Whitney Brown    15 TRUE             NA        NA FALSE                 20
6 A. Whitney Brown    16 TRUE             NA        NA FALSE                 20
# ℹ 1 more variable: season_fraction <dbl>
head(snl_actors)
# A tibble: 6 × 4
  aid            url           type  gender 
  <chr>          <chr>         <chr> <chr>  
1 Kate McKinnon  /Cast/?KaMc   cast  female 
2 Alex Moffat    /Cast/?AlMo   cast  male   
3 Ego Nwodim     /Cast/?EgNw   cast  unknown
4 Chris Redd     /Cast/?ChRe   cast  male   
5 Kenan Thompson /Cast/?KeTh   cast  male   
6 Carey Mulligan /Guests/?3677 guest andy   

Briefly Describe the data

The dataset snl_seasons contains 46 observations about the different seasons of “Saturday Night Live”. The three datasets consist of actors, casts, and seasons, which contain details about each actor, type, and gender.

library(summarytools)

print(summarytools::dfSummary(snl_seasons,
                        varnumbers = FALSE,
                        plain.ascii  = FALSE, 
                        style        = "grid", 
                        graph.magnif = 0.70, 
                        valid.col    = FALSE),
      method = 'render',
      table.classes = 'table-condensed')

Data Frame Summary

snl_seasons

Dimensions: 46 x 5
Duplicates: 0
Variable Stats / Values Freqs (% of Valid) Graph Missing
sid [numeric]
Mean (sd) : 23.5 (13.4)
min ≤ med ≤ max:
1 ≤ 23.5 ≤ 46
IQR (CV) : 22.5 (0.6)
46 distinct values 0 (0.0%)
year [numeric]
Mean (sd) : 1997.5 (13.4)
min ≤ med ≤ max:
1975 ≤ 1997.5 ≤ 2020
IQR (CV) : 22.5 (0)
46 distinct values 0 (0.0%)
first_epid [numeric]
Mean (sd) : 19975965 (134209.3)
min ≤ med ≤ max:
19751011 ≤ 19975926 ≤ 20201003
IQR (CV) : 224910.2 (0)
46 distinct values 0 (0.0%)
last_epid [numeric]
Mean (sd) : 19985509 (134223.9)
min ≤ med ≤ max:
19760731 ≤ 19985512 ≤ 20210410
IQR (CV) : 225066.2 (0)
46 distinct values 0 (0.0%)
n_episodes [numeric]
Mean (sd) : 19.7 (2.3)
min ≤ med ≤ max:
12 ≤ 20 ≤ 24
IQR (CV) : 1 (0.1)
12 : 1 ( 2.2% )
13 : 2 ( 4.3% )
17 : 2 ( 4.3% )
18 : 2 ( 4.3% )
19 : 3 ( 6.5% )
20 : 23 ( 50.0% )
21 : 7 ( 15.2% )
22 : 5 ( 10.9% )
24 : 1 ( 2.2% )
0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.2)
2023-05-05

print(summarytools::dfSummary(snl_casts,
                        varnumbers = FALSE,
                        plain.ascii  = FALSE, 
                        style        = "grid", 
                        graph.magnif = 0.70, 
                        valid.col    = FALSE),
      method = 'render',
      table.classes = 'table-condensed')

Data Frame Summary

snl_casts

Dimensions: 614 x 8
Duplicates: 0
Variable Stats / Values Freqs (% of Valid) Graph Missing
aid [character]
1. Kenan Thompson
2. Darrell Hammond
3. Seth Meyers
4. Al Franken
5. Fred Armisen
6. Kate McKinnon
7. Tim Meadows
8. Aidy Bryant
9. Bobby Moynihan
10. Cecily Strong
[ 146 others ]
18 ( 2.9% )
14 ( 2.3% )
13 ( 2.1% )
11 ( 1.8% )
11 ( 1.8% )
10 ( 1.6% )
10 ( 1.6% )
9 ( 1.5% )
9 ( 1.5% )
9 ( 1.5% )
500 ( 81.4% )
0 (0.0%)
sid [numeric]
Mean (sd) : 25.5 (13.1)
min ≤ med ≤ max:
1 ≤ 26 ≤ 46
IQR (CV) : 22 (0.5)
46 distinct values 0 (0.0%)
featured [logical]
1. FALSE
2. TRUE
451 ( 73.5% )
163 ( 26.5% )
0 (0.0%)
first_epid [numeric]
Mean (sd) : 19909634 (111264.8)
min ≤ med ≤ max:
19770115 ≤ 19901110 ≤ 20141025
IQR (CV) : 156624.2 (0)
35 distinct values 564 (91.9%)
last_epid [numeric]
Mean (sd) : 19944038 (126122.2)
min ≤ med ≤ max:
19751011 ≤ 19950225 ≤ 20140201
IQR (CV) : 190005 (0)
17 distinct values 597 (97.2%)
update_anchor [logical]
1. FALSE
2. TRUE
541 ( 88.1% )
73 ( 11.9% )
0 (0.0%)
n_episodes [numeric]
Mean (sd) : 18.7 (4)
min ≤ med ≤ max:
1 ≤ 20 ≤ 24
IQR (CV) : 2 (0.2)
22 distinct values 0 (0.0%)
season_fraction [numeric]
Mean (sd) : 0.9 (0.2)
min ≤ med ≤ max:
0 ≤ 1 ≤ 1
IQR (CV) : 0 (0.2)
36 distinct values 0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.2)
2023-05-05

print(summarytools::dfSummary(snl_actors,
                        varnumbers = FALSE,
                        plain.ascii  = FALSE, 
                        style        = "grid", 
                        graph.magnif = 0.70, 
                        valid.col    = FALSE),
      method = 'render',
      table.classes = 'table-condensed')

Data Frame Summary

snl_actors

Dimensions: 2306 x 4
Duplicates: 0
Variable Stats / Values Freqs (% of Valid) Graph Missing
aid [character]
1. 'N Sync
2. 070 Shake
3. 10,000 Maniacs
4. 14 Karat Soul
5. 2 Chainz
6. 3-D
7. 3RDEYEGIRL
8. 50 Cent
9. A Tribe Called Quest
10. A. Whitney Brown
[ 2296 others ]
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
2296 ( 99.6% )
0 (0.0%)
url [character]
1. /Cast/?AbEl
2. /Cast/?AdSa
3. /Cast/?AiBr
4. /Cast/?AlFr
5. /Cast/?AlMo
6. /Cast/?AlZw
7. /Cast/?AmPo
8. /Cast/?AnDi
9. /Cast/?AnGa
10. /Cast/?AnMH
[ 2239 others ]
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
1 ( 0.0% )
2239 ( 99.6% )
57 (2.5%)
type [character]
1. cast
2. crew
3. guest
4. unknown
154 ( 6.7% )
170 ( 7.4% )
1926 ( 83.5% )
56 ( 2.4% )
0 (0.0%)
gender [character]
1. andy
2. female
3. male
4. unknown
21 ( 0.9% )
671 ( 29.1% )
1226 ( 53.2% )
388 ( 16.8% )
0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.2)
2023-05-05

Tidy Data (as needed)

To tidy the dataset I rename all columns in each data set.

# Renaming columns in seasons
snl_seasons <- snl_seasons %>%
  rename(
    Season = sid,
    Year = year,
    FirstEpisode = first_epid,
    LastEpisode = last_epid,
    TotEpisodes = n_episodes,
  )

# Renaming columns in casts
snl_casts <- snl_casts %>%
  rename(
    Actor = aid,
    Season = sid,
    Featured = featured,
    Anchor = update_anchor,
    Episodes = n_episodes,
    EpisodesProp = season_fraction
  )
# Renaming columns in actors
snl_actors <- snl_actors %>% 
  rename(
    Actor = aid,
    Type = type,
    Gender = gender
    )

# Mutate fields
snl_actors <- snl_actors %>%
  mutate(Gender = str_replace(Gender, "andy", "unknown"), Type = str_replace(Type, "unknown", "celebrity")
  )

Join Data

Be sure to include a sanity check, and double-check that case count is correct!

library(dplyr)
snl_joined = merge(x=snl_casts, y=snl_actors, by.y = "aid", by.x = "aid")
Error in fix.by(by.x, x): 'by' must specify a uniquely valid column
print(summarytools::dfSummary(snl_joined,
                        varnumbers = FALSE,
                        plain.ascii  = FALSE, 
                        style        = "grid", 
                        graph.magnif = 0.70, 
                        valid.col    = FALSE),
      method = 'render',
      table.classes = 'table-condensed')
Error in summarytools::dfSummary(snl_joined, varnumbers = FALSE, plain.ascii = FALSE, : object 'snl_joined' not found