Case study

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

chap_5_1.002.png

String Manipulation with stringr in R

chap_5_1.003.png

String Manipulation with stringr in R

chap_5_1.004.png

String Manipulation with stringr in R

readLines()

old_mac <- readLines("old_mac.txt")

str(old_mac)
chr [1:7] "Old MacDonald had a farm" ...
old_mac[1:2]
"Old MacDonald had a farm" "E-I-E-I-O"
str_detect(old_mac, "moo")
FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
which(str_detect(old_mac, "moo"))
5

    old_mac.txt

Old MacDonald had a farm
E-I-E-I-O
And on his farm he had a cow
E-I-E-I-O
Here a moo, There a moo, 
Everywhere a moo-moo
Old MacDonald had a farm
E-I-E-I-O
  • Alternatively:
    stringi::stri_read_lines()
    
String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...