I am looking at a novel and want to search for the appearance of characters' names throughout the book Some characters go by different names. For example, the character "Sissy Jupe" goes by "Sissy" and "Jupe". I want to combine two rows of word counts into one so I can see the tally for "Sissy Jupe".
I've looked at using sum, rbind, merge, and other approaches using the message boards, but nothing seems to work. Lots of great examples, but they aren't working.
library(tidyverse)
library(gutenbergr)
library(tidytext)
ht <- gutenberg_download(786)
ht_chap <- ht %>%
mutate(linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
ignore_case = TRUE))))
tidy_ht <- ht_chap %>%
unnest_tokens(word, text) %>%
mutate(word = str_extract(word, "[a-z']+")) # preserves online letters; removes _)
ht_count <- tidy_ht %>%
group_by(chapter) %>%
count(word, sort = TRUE) %>%
ungroup %>%
complete(chapter, word,
fill = list(n = 0))
gradgrind <- filter(ht_count, word == "gradgrind")
bounderby <- filter (ht_count, word == "bounderby")
sissy <- filter (ht_count, word == "sissy")
## TEST
sissy_jupe <- ht_count %>%
filter(word %in% c("sissy", "jupe"))
I want a single "word" item called "sissy_jupe" that tallies the n by chapter. This is close, but not it.
# A tibble: 76 x 3
chapter word n
<int> <chr> <dbl>
1 0 jupe 0
2 0 sissy 1
3 1 jupe 0
4 1 sissy 0
5 2 jupe 5
6 2 sissy 9
7 3 jupe 3
8 3 sissy 1
9 4 jupe 1
10 4 sissy 0
# … with 66 more rows