Chapter 11 Manipulating Strings

Lander's chapter 13 Manipulating Strings

################################
#Chapter13 Manipulating Strings#
################################

#paste
paste("Hello", "Jared", "and others")
## [1] "Hello Jared and others"
paste("Hello", "Jared", "and others", sep = "/")
## [1] "Hello/Jared/and others"
#vectorized-paired one-to-one
paste(c("Hello", "Hey", "Howdy"), c("Jared", "Bob", "David"))
## [1] "Hello Jared" "Hey Bob"     "Howdy David"
#recycle-if have different lengthes
paste("Hello", c("Jared", "Bob", "David"))
## [1] "Hello Jared" "Hello Bob"   "Hello David"
paste("Hello", c("Jared", "Bob", "David"), c("Goodbye", "Seeya"))
## [1] "Hello Jared Goodbye" "Hello Bob Seeya"     "Hello David Goodbye"
#collapse a vector
vectorOfText <- c("Hello", "Everyone", "out there", ".")
vectorOfText
## [1] "Hello"     "Everyone"  "out there" "."
paste(vectorOfText, collapse = " ")
## [1] "Hello Everyone out there ."
paste(vectorOfText, collapse = "*")
## [1] "Hello*Everyone*out there*."
#sprintf
person <- "Jared"
partySize <- "eight"
waitTime <- 25
paste("Hello ", person, ", your party of ", partySize, " will be seated in ", waitTime, " minutes.", sep = "")
## [1] "Hello Jared, your party of eight will be seated in 25 minutes."
sprintf("Hello %s, your party of %s will be seated in %s minutes", person, partySize, waitTime)
## [1] "Hello Jared, your party of eight will be seated in 25 minutes"
#note: vector lengths must be multiples of each other
sprintf("Hello %s, your party of %s will be seated in %s minutes",
        c("Jared", "Bob"), c("eight", 16, "four", 10), waitTime)
## [1] "Hello Jared, your party of eight will be seated in 25 minutes"
## [2] "Hello Bob, your party of 16 will be seated in 25 minutes"     
## [3] "Hello Jared, your party of four will be seated in 25 minutes" 
## [4] "Hello Bob, your party of 10 will be seated in 25 minutes"