How to Search and Filter Text in R

In the video and code below I demonstrate how to use R to search and filter text. For examples I use the Friends package which contains the transcripts of all of the episodes of the Friends TV show.


Subscribe below to get updates on my latest videos, courses, and other useful information.


library(friends)
library(tidyverse)

data(friends)

# find "on a break"
friends %>% filter(grepl("on a break", text))

# Find "how you doin'" base and tidyverse
friends[(grepl("how you doin'", friends$text)),]
friends %>% filter(grepl("how you doin'", text))

# ^ to match the start of the string and $ to match the end of the string
friends %>% filter(grepl("Hey", text))
friends %>% filter(grepl("^Hey$", text))

# exact match - just user filter but not grepl (don't use the first one here)
friends %>% filter(grepl("Joey", speaker))  # don't do this!!
friends %>% filter(speaker == "Joey Tribbiani") 

library(janitor)
friends %>% filter(grepl("Joey", speaker)) %>% tabyl(speaker)