A case study on case

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

regex are case sensitive

  • "dog" won't match "Dog"
  • Accidents involving cats: catcidents

chap_5_2.005.png

String Manipulation with stringr in R

regex are case sensitive

  • "dog" won’t match "Dog"
  • Accidents involving cats: catcidents

chap_5_2.006.png

String Manipulation with stringr in R

regex are case sensitive

  • "dog" won't match "Dog"
  • Accidents involving cats: catcidents

chap_5_2.007.png

String Manipulation with stringr in R

regex are case sensitive

  • "dog" won't match "Dog"
  • Accidents involving cats: catcidents

chap_5_2.008.png

String Manipulation with stringr in R

Case sensitive matching

str_subset(catcidents, "food")
"3Yof-foot lac-cut on cat food can-@ home "     
"4 Yom was cut on cat food can. Dx:  r index lac 1 cm."
str_subset(catcidents, "Food")
"17Yof Cut Right Hand On A Cat Food Can - Laceration "            
"Pt Lifted Bag Of Cat Food. Dx:  Low Back Px, Hx Arthritic Spine."
str_subset(catcidents, "fOOd")
"LaC FInGer oN a meTAL Cat fOOd CaN "
String Manipulation with stringr in R

Change case of input

str_subset(str_to_lower(catcidents), "food")
"21 yof reports sus laceration of her left hand when she ..."
"3yof-foot lac-cut on cat food can-@ home "                 
"15 mo m cut finger on cat food can lid. dx:  r index lac..."   
"accidentally cut finger while opening a cat food can,..."
"4 yom was cut on cat food can. dx:  r index lac 1 cm."    
"17yof cut right hand on a cat food can - laceration "   
"50yof cut finger on cat food can lid.  dx: lt ring ..."   
String Manipulation with stringr in R

Use case insensitive matching

str_subset(catcidents, regex("food", ignore_case = TRUE)) 
"21 YOF REPORTS SUS LACERATION OF HER LEFT HAND WHEN SHE ..."
"3Yof-foot lac-cut on cat food can-@ home " 
"15 mO m cut FinGer ON cAT FoOd CAn LID. Dx:  r INDeX laC..." 
"ACCIDENTALLY CUT FINGER WHILE OPENING A CAT FOOD CAN, ..."
"4 Yom was cut on cat food can. Dx:  r index lac 1 cm."
"17Yof Cut Right Hand On A Cat Food Can - Laceration "  
"50YOF CUT FINGER ON CAT FOOD CAN LID.  DX: LT RING ..."  
"LaC FInGer oN a meTAL Cat fOOd CaN "                  
"10 YO FEMALE OPENING A CAN OF CAT FOOD.  DX HAND ..."
"Pt Lifted Bag Of Cat Food. Dx:  Low Back Px, Hx ..."
String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...