R paste() Function

The paste() function in R concatenates vectors after converting them to character.

How to use the paste() function in R

paste("Hello", 19, 21, "Mate")

# Output: [1] "Hello 19 21 Mate"

It helps us combine multiple strings or vectors into a single string or vector of strings, with customizable separators, constructing file paths and URLs, creating composite keys from multiple variables, manipulating text data, formatting strings, or generating dynamic text.

Syntax

paste(data, sep=" ", collapse=NULL)

Parameters

Argument Description
data It is one or more R objects to be concatenated together.
sep It is a character string to separate the terms.
collapse It is NULL. It is an optional character string to separate the results.

Passing the “separator”

If you pass the separator argument to this function, it will separate the elements.

Passing separator argument to paste() function

paste("Hello", 19, 21, "Mate", sep = "_")

# Output: [1] "Hello_19_21_Mate"

Collapsing a vector of strings

The collapse argument can define a value to use when joining those values to create a single string.

paste(c("K", "D", "L"), 1:9, sep = "|", collapse = " & ")

# Output: [1] "K|1 & D|2 & L|3 & K|4 & D|5 & L|6 & K|7 & D|8 & L|9"

Concatenating text and a variable

Figure of Concatenating text and variable in R

name <- "Krunal"
age <- 30

paste("My name is", name, "and age is", age)

# Output: [1] "My name is Krunal and age is 30"

Difference between paste and paste0()

The main difference between paste() and paste0() is that the paste() function uses a space (” “) as a default separator between the strings, whereas the paste0() function uses no space as a separator, which means it concatenates strings without any separator.

paste("hello", "mate", "how", "are", "you")

paste0("hello", "mate", "how", "are", "you")

# Output:

# [1] "hello mate how are you"

# [1] "hellomatehowareyou"

Handling non-character inputs

If the inputs are non-characters and you pass them to the paste() method, it will coerce them to characters.

num_vec <- c(1, 2, 3)
bool_vec <- c(TRUE, FALSE, TRUE)

result <- paste(num_vec, bool_vec, sep = ":")

print(result)

# Output: [1] "1:TRUE" "2:FALSE" "3:TRUE"

Vector recycling

If you have two vectors with different lengths, shorter vectors are recycled to match the length of the longest vector.

longer_vec <- c("A", "B", "C")
shorted_vec <- "x"

combined_vec <- paste(longer_vec, shorted_vec, sep = "_")

print(combined_vec)

# Output: [1] "A_x" "B_x" "C_x"

Empty input

If you pass the empty input, the output will be character(0).

empty_output <- paste()

print(empty_output)

# Output: character(0)

NULL or NA values

The paste() function will ignore NULL and treat NA as the string “NA”.

output_with_null <- paste("Kaynes", NULL, "Technology")

print(output_with_null)
# Output: [1] "Kaynes Technology"

output_with_na <- paste("Kaynes", NA, "Technology")

print(output_with_na)
# Output: [1] "Kaynes NA Technology"

Zero-length vectors

If you pass 0 0-length vector like character(0) to the paste() function, it returns character(0), which means empty.

zero_length <- character(0)

result <- paste(zero_length, "amber")

result

# Output: [1] " amber"

That’s it!

Leave a Comment