R Basic

Converting Vector to String in R

You may want to convert a vector to a string when you need to combine vector elements into a single character string for data export. For example, exporting into csv, text, or database files.

Here are three ways to convert a vector to a string in R:

  1. Using paste()
  2. Using paste0()
  3. Using toString()

Method 1: Using paste()

For combining single or multiple vectors into a single string, you can use the paste() function with a specified separator (sep) and optional collapsing.

vec <- c("Ahsoka", "Din", "Grogu")

mando <- paste(vec, collapse = " ")

mando

# Output: [1] "Ahsoka Din Grogu"

You can use the collapse” argument to specify the delimiter between each word in the vector.

vec <- c("Krunal", "Ankit", "Rushabh", "Dhaval")

# Convert vector to string
str <- paste(vec, collapse = "--")

str

# Output: [1] "Krunal--Ankit--Rushabh--Dhaval"

Multiple vectors

What if you are working with multiple vectors? Since the paste() function accepts multiple vectors, it will return a single string containing multiple vectors.

vec1 <- c("x", "k")
vec2 <- c(19, 21)

combined_string <- paste(vec1, vec2, sep = "=", collapse = "; ")

combined_string

# Output: [1] "x=19; k=21"

Empty vector

If the input is an empty vector, the output will be an empty string.

empty_vec <- character(0)

empty_str <- paste(vec, collapse = ", ")

print(empty_str)

# Output: ""

Vector containing NULL or NA

If the input vector contains NA values, the output will have NA values as a string.

vec_with_na <- c("babli", NA, "bb", "fameer", "hola")

empty_str_with_na <- paste(vec_with_na, collapse = ", ")

print(empty_str_with_na)

# Output: [1] "babli, NA, bb, fameer, hola"

If you want to handle NA values, use na.omit() or is.na() methods.

Method 2: Using paste0()

The paste0() method is an optimized version of the paste() method with no separator.

numeric_vec <- c(11, 2, 31, 4, 15)

character_vec <- c("bmw", "maruti", "tata")

logical_vec <- c(TRUE, FALSE, TRUE)

paste0(character_vec, collapse = "")
# Output: [1] "bmwmarutitata"

paste0(logical_vec, collapse = "-")
# Output: [1] "TRUE-FALSE-TRUE"

paste0(numeric_vec, collapse = "")
# Output: [1] "11231415"

Method 3: Using toString()

vec <- c("Ahsoka", "Din", "Grogu")

str <- toString(vec)

print(str)

# Output: [1] "Ahsoka, Din, Grogu"

Let’s check the data type.

rv <- c("Ahsoka", "Din", "Grogu")

mando <- toString(rv)

print(typeof(mando))

# Output: [1] "character"

Performance test for different methods

Using system.time(), we can compare the performance of all three methods and conclude which is the fastest method when the size of a vector is enormous.

large_vec <- 1:10000

# Timing different approaches
system.time({
  result1 <- paste(large_vec, collapse = ", ")
})

system.time({
  result2 <- toString(large_vec)
})

system.time({
  result3 <- paste0(large_vec, collapse = ", ")
})

# Output:

# user system elapsed
# 0.003 0.000 0.003

# user system elapsed
# 0.003 0.000 0.003

# user system elapsed
# 0.003 0.000 0.003

You can see that all these methods took the same time. That means you can choose any method when it comes to performance.

That’s all!

Recent Posts

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

15 hours ago

R paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

1 week ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

1 week ago

How to Calculate Standard Error in R

Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

2 weeks ago

R max() and min() Functions

max() The max() function in R finds the maximum value of a vector or data…

2 weeks ago

R as.Date() Function: Working with Dates

The as.Date() function in R converts various types of date and time objects or character…

3 weeks ago