Created
February 3, 2021 19:03
-
-
Save erikgregorywebb/2e305c2663420e348eeed93c1994272c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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