Skip to content

Instantly share code, notes, and snippets.

@erikgregorywebb
Created February 3, 2021 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikgregorywebb/2e305c2663420e348eeed93c1994272c to your computer and use it in GitHub Desktop.
Save erikgregorywebb/2e305c2663420e348eeed93c1994272c to your computer and use it in GitHub Desktop.
# define character count function
get_character_counts = function(df, show_name, character_name) {
total_episodes = df %>% filter(show == show_name) %>% nrow()
character_episodes = df %>%
filter(show == show_name) %>% filter(str_detect(description, character_name)) %>% nrow()
result = tibble(show = show_name, character_name = character_name,
total_episodes = total_episodes, character_episodes = character_episodes)
return(result)
}
# define looping function
get_all_character_counts = function(df, show_name, character_names) {
datalist = list()
for (i in 1:length(character_names)) {
datalist[[i]] = get_character_counts(all_shows, show_name, character_names[i])
}
raw = do.call(rbind, datalist)
return(raw)
}
# the office
names = c('Michael', 'Dwight', 'Jim', 'Pam', 'Ryan', 'Andy', 'Oscar', 'Stanley', 'Kevin', 'Meredith', 'Angela', 'Phyllis', 'Toby', 'Kelly', 'Darryl')
the_office = get_all_character_counts(all_shows, 'The Office', names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment