Putting strings together

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

paste()

paste("E", "I", "E", "I", "O")
"E I E I O"
paste("E", "I", "E", "I", "O", sep = "-")
"E-I-E-I-O"
paste(c("Here", "There", "Everywhere"), "a")
"Here a"       "There a"      "Everywhere a"

chap_1_3.008.png

String Manipulation with stringr in R

chap_1_3.009.png

String Manipulation with stringr in R

paste()

animal_goes <- "moo"

paste(c("Here", "There", "Everywhere"), "a", animal_goes)
"Here a moo"       "There a moo"      "Everywhere a moo"
paste(c("Here", "There", "Everywhere"), "a", animal_goes,
        collapse = ", ")
"Here a moo, There a moo, Everywhere a moo"
paste(c("Here", "There", "Everywhere"), "a", 
      c(animal_goes, animal_goes,
        paste(rep(animal_goes, 2), collapse = "-")),
      collapse = ", ")
"Here a moo, There a moo, Everywhere a moo-moo"
String Manipulation with stringr in R

paste()

old_mac <- function(animal, animal_goes){
  eieio <- paste("E", "I", "E", "I", "O", sep = "-")
  old_mac <- "Old MacDonald had a farm"
  writeLines(c(
    old_mac,
    eieio,
    paste("And on his farm he had a", animal),
    eieio,
    paste(c("Here", "There", "Everywhere"), "a", 
      c(animal_goes, animal_goes,
        paste(rep(animal_goes, 2), collapse = "-")),
      collapse = ", "),
    old_mac,
    eieio))
}
String Manipulation with stringr in R
old_mac("cow", "moo")
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
old_mac("dog", "woof")
Old MacDonald had a farm
E-I-E-I-O
And on his farm he had a dog
E-I-E-I-O
Here a woof, There a woof, Everywhere a woof-woof
Old MacDonald had a farm
E-I-E-I-O
String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...