This tutorial explains how to work with strings in R using the functions paste, paste0, and sprintf.

**Using paste & paste0**

The **paste()** function converts arguments into character strings and concatenates them using the following syntax:

**paste(…, sep = ” “, collapse = NULL)**

- … – one or more R objects to be converted to characters
- sep – a character string to separate the terms
- collapse – a character string to separate the results. This is optional.

The **paste0()** function is the shorthand version of paste(…, sep = “”, collapse = NULL).

The following code illustrates several examples of paste() and paste0() in action.

#create two string variablesa <- "dolphin" b <- "manatee"#concatenateaandbtogether with a space in betweenpaste(a, b) #[1] "dolphin manatee"#concatenateaandbtogether with a comma and space in betweenpaste(a, b, sep = ", ") #[1] "dolphin, manatee"#concatenateaandbtogether with no space in between using paste()paste(a, b, sep = "") #[1] "dolphinmanatee"#concatenateaandbtogether with no space in between using paste0()paste0(a, b) #[1] "dolphinmanatee"#create a vector and concatenate all elements in vector into one stringx <- c("dog", "cat", "bird") paste(x) #[1] "dog" "cat" "bird"#concatenate all elements in vector using a comma to collapse them togetherpaste(x, collapse = ", ") #[1] "dog, cat, bird"#concatenate all elements in vector using a dash to collapse them togetherpaste(x, collapse = " - ") #[1] "dog - cat - bird"#concatenate a character variable with a vector of character variablespaste(a, x) #[1] "dolphin dog" "dolphin cat" "dolphin bird"#same example, but use a * toseparatewordspaste(a, x, sep = "*") #[1] "dolphin*dog" "dolphin*cat" "dolphin*bird"#same example, but also use a comma to collapse the elementspaste(a, x, sep = "*", collapse = ", ") #[1] "dolphin*dog, dolphin*cat, dolphin*bird"

**Using sprintf**

The **sprintf()** function is similar to the paste() function, but with slightly different syntax:

**paste(fmt, …)**

- fmt – a character vector of format strings
- … – values to be passed into fmt

The following code illustrates several examples of sprintf() in action.

#create a string variable x <- "name" #substitute a string variable into a phrase using %s sprintf("This is where your %s belongs.", x) #[1] "This is where your name belongs." #insert several string variables using %s sprintf("I need to get there by %s:%s%s a.m.", 8, 1, 5) # [1] "I need to get there by 8:15 a.m." #alternatively, define each variable to be placed in the phrase hour <- 8 minsTen <- 1 minsSingle <- 5 sprintf("I need to get there by %s:%s%s a.m.", hour, minsTen, minsSingle) # [1] "I need to get there by 8:15 a.m." #substitute an integer variable into a phrase using %d x <- 25 sprintf("I am %d years old.", x) #[1] "I am 25 years old."

For floating point numbers, you may use a variety of different notations depending on what you’d like your output to look like:

**%f**– use for standard notation**%e**or**%E**– use for exponential notation**%g**or**%G**– use for a “smart” formatter that formats numbers based on significant digits

The following examples illustrate these different formats using the mathematical number *pi*. These examples are taken from the R help page for sprintf.

sprintf("%f", pi) # "3.141593"sprintf("%.3f", pi) # "3.142"sprintf("%1.0f", pi) # "3"sprintf("%5.1f", pi) # " 3.1"sprintf("%05.1f", pi) # "003.1"sprintf("%+f", pi) # "+3.141593"sprintf("% f", pi) # " 3.141593"sprintf("%-10f", pi) # "3.141593 " (left justified)sprintf("%e", pi) #"3.141593e+00"sprintf("%E", pi) # "3.141593E+00"sprintf("%g", pi) # "3.14159"sprintf("%g", 1e6 * pi) # "3.14159e+06" (exponential)sprintf("%.9g", 1e6 * pi) # "3141592.65" ("fixed")sprintf("%G", 1e-6 * pi) # "3.14159E-06"