Introducing stringr String Manipulation with stringr stringr - - PowerPoint PPT Presentation

introducing stringr
SMART_READER_LITE
LIVE PREVIEW

Introducing stringr String Manipulation with stringr stringr - - PowerPoint PPT Presentation

STRING MANIPULATION WITH STRINGR Introducing stringr String Manipulation with stringr stringr Powerful but easy to learn Built on stringi Concise and consistent All functions start with str_ All functions take a vector


slide-1
SLIDE 1

STRING MANIPULATION WITH STRINGR

Introducing stringr

slide-2
SLIDE 2

String Manipulation with stringr

stringr

  • Powerful but easy to learn
  • Built on stringi
  • Concise and consistent
  • All functions start with str_
  • All functions take a vector of strings as the first

argument

slide-3
SLIDE 3

String Manipulation with stringr

str_c()

> my_toppings [1] "green peppers" "olives" "onions"

slide-4
SLIDE 4

String Manipulation with stringr

str_c()

> my_toppings [1] "green peppers" "olives" "onions" > paste(c("", "", "and "), my_toppings, sep = "") [1] "green peppers" "olives" "and onions"

slide-5
SLIDE 5

String Manipulation with stringr

str_c()

> my_toppings [1] "green peppers" "olives" "onions" > paste(c("", "", "and "), my_toppings, sep = "") [1] "green peppers" "olives" "and onions" > library(stringr) > str_c(c("", "", "and "), my_toppings) [1] "green peppers" "olives" "and onions"

slide-6
SLIDE 6

String Manipulation with stringr

str_c()

> my_toppings [1] "green peppers" "olives" "onions" > paste(c("", "", "and "), my_toppings, sep = "") [1] "green peppers" "olives" "and onions"

str_length(), str_sub()

> library(stringr) > str_c(c("", "", "and "), my_toppings) [1] "green peppers" "olives" "and onions"

slide-7
SLIDE 7

String Manipulation with stringr

Babynames

  • USA from 1880 to 2014
  • You'll use 2014 only

> library(babynames) > head(babynames) year sex name n prop 1 1880 F Mary 7065 0.07238359 2 1880 F Anna 2604 0.02667896 3 1880 F Emma 2003 0.02052149 4 1880 F Elizabeth 1939 0.01986579 5 1880 F Minnie 1746 0.01788843 6 1880 F Margaret 1578 0.01616720

slide-8
SLIDE 8

STRING MANIPULATION WITH STRINGR

Let’s practice!

slide-9
SLIDE 9

STRING MANIPULATION WITH STRINGR

Hunting for matches

slide-10
SLIDE 10

String Manipulation with stringr

stringr functions that look for matches

  • All take a pattern argument
  • str_detect()
  • str_subset()
  • str_count()
slide-11
SLIDE 11

String Manipulation with stringr

Finding matches

> pizzas <- c("cheese", "pepperoni", "sausage and green peppers") > str_detect(string = pizzas, pattern = "pepper") [1] FALSE TRUE TRUE > str_detect(string = pizzas, pattern = fixed("pepper")) [1] FALSE TRUE TRUE > str_subset(string = pizzas, pattern = fixed("pepper")) [1] "pepperoni" "sausage and green peppers" > str_count(string = pizzas, pattern = fixed("pepper")) [1] 0 1 1

slide-12
SLIDE 12

STRING MANIPULATION WITH STRINGR

Let’s practice!

slide-13
SLIDE 13

STRING MANIPULATION WITH STRINGR

Spliing strings

slide-14
SLIDE 14

String Manipulation with stringr

str_split()

> str_split(string = "Tom & Jerry", pattern = " & ") [[1]] [1] "Tom" "Jerry" > str_split("Alvin & Simon & Theodore", pattern = " & ") [[1]] [1] "Alvin" "Simon" "Theodore" > str_split("Alvin & Simon & Theodore", pattern = " & ", n = 2) [[1]] [1] "Alvin" "Simon & Theodore"

"Tom & Jerry" & not of interest

slide-15
SLIDE 15

String Manipulation with stringr

str_split() returns a list

> chars <- c("Tom & Jerry", "Alvin & Simon & Theodore") > str_split(chars, pattern = " & ") [[1]] [1] "Tom" "Jerry" [[2]] [1] "Alvin" "Simon" "Theodore"

slide-16
SLIDE 16

String Manipulation with stringr

str_split() can return a matrix

> chars <- c("Tom & Jerry", "Alvin & Simon & Theodore") > str_split(chars, pattern = " & ", simplify = TRUE) [,1] [,2] [,3] [1,] "Tom" "Jerry" "" [2,] "Alvin" "Simon" "Theodore"

slide-17
SLIDE 17

String Manipulation with stringr

Combing with lapply()

> chars <- c("Tom & Jerry", "Alvin & Simon & Theodore") > split_chars <- str_split(chars, pattern = " & ") > split_chars [[1]] [1] "Tom" "Jerry" [[2]] [1] "Alvin" "Simon" "Theodore" > lapply(split_chars, length) [[1]] [1] 2 [[2]] [1] 3

slide-18
SLIDE 18

STRING MANIPULATION WITH STRINGR

Let’s practice!

slide-19
SLIDE 19

STRING MANIPULATION WITH STRINGR

Replacing matches in strings

slide-20
SLIDE 20

String Manipulation with stringr

str_replace()

> str_replace("Tom & Jerry", pattern = "&", replacement = "and") [1] "Tom and Jerry" > str_replace("Alvin & Simon & Theodore", pattern = "&", replacement = "and") [1] "Alvin and Simon & Theodore" > str_replace_all("Alvin & Simon & Theodore", pattern = "&", replacement = "and") [1] "Alvin and Simon and Theodore"

slide-21
SLIDE 21

String Manipulation with stringr

str_replace() with vectors

> chars <- c("Tom & Jerry", "Alvin & Simon & Theodore") > str_replace_all(chars, pattern = "&", replacement = "and") [1] "Tom and Jerry" [2] "Alvin and Simon and Theodore"

slide-22
SLIDE 22

STRING MANIPULATION WITH STRINGR

Let’s practice!