Working with Strings

Stat 251

2025-03-25

Working with regular expressions

str_view in stringr package (in R) shows all matches:

stringr::str_view("> Poem for a Friend. ", pattern = "\\W")
[1] │ <>>< >Poem< >for< >a< >Friend<.>< >
stringr::str_view("> Poem for a Friend. ", pattern = "\\b")
[1] │ > <>Poem<> <>for<> <>a<> <>Friend<>. 

str_count counts all matches in R:

stringr::str_count("> Poem for a Friend. ", pattern = "\\b")/2
[1] 4

find_all and len count all matches in python:

import re
res = re.findall("\\b", "> Poem for a Friend. ")
len(res)/2
4.0

Idea for Exploration

Run summary statistics for second poet. Then compare.

Homework 8

Pivoting with dogs :)