Hunting for matches

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

stringr functions that look for matches

  • All take a pattern argument
    • str_detect()
    • str_subset()
    • str_count()
String Manipulation with stringr in R

Finding matches

pizzas <- c("cheese", "pepperoni", "sausage and green peppers")

str_detect(string = pizzas, pattern = "pepper")
FALSE  TRUE  TRUE
str_detect(string = pizzas, pattern = fixed("pepper"))
FALSE  TRUE  TRUE
String Manipulation with stringr in R

Finding matches

str_subset(string = pizzas, pattern = fixed("pepper"))
"pepperoni" "sausage and green peppers"
str_count(string = pizzas, pattern = fixed("pepper"))
0 1 1
String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...