Just say I want to cont the number of "a"'s and "p"'s in the word "apple", I can do:
library(stringr)
sum(str_count("apple", c("b", "k")))
but when I try to apply this logic in order to count the number of "a"'s and "p"'s across multiple different words in a variable in a dataframe, it doesn't work, e.g.:
dat <- tibble(id = 1:4, word = c(c("apple", "banana", "pear", "pineapple")))
dat <- dat %>% mutate(num_ap = sum(str_count(word, c("a", "p"))))
it doesn't work. I the variable "num_ap" should read c(3, 3, 2, 4) but instead it reads c(5, 5, 5, 5)
Does anyone know why this isn't working for me?
Thanks!