Strings

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

Strings are text data stored in character vectors.

Paste function can "paste" text into string elements

# Create a character vector with three strings
meals <- c("cheeseburger", "soup", "sandwich")
# Paste "bacon" onto the end of each string element
bacon.meals <- paste(meals, "bacon"); bacon.meals
[1] "cheeseburger bacon" "soup bacon"         "sandwich bacon"
bacon.meals.dash <- paste(meals, "bacon", sep="-"); bacon.meals.dash; bacon.meals.dash
[1] "cheeseburger-bacon" "soup-bacon"         "sandwich-bacon"






[1] "cheeseburger-bacon" "soup-bacon"         "sandwich-bacon"
# We can also collapse the whole vector into a single string
mouthful <- paste(meals, collapse=""); mouthful
[1] "cheeseburgersoupsandwich"