You are currently viewing R Convert List to String with Examples

How to convert the list to String in R? The list can contain elements of different types for example strings, numeric, vector, matrix, data.frame. And if you have values in double quotes it considers as a String, use paste() or toString() methods to merge all elements from the list into a string value with or without a delimiter.

The following methods are used to convert elements from the list to string in R programming.

  • Convert by using paste()
  • Convert by using toString()

1. Using paste() to Convert List to String

The paste() function in R can be used to convert or merge a list of elements into a string by separating each element by a delimiter. To separate the string elements from a single list using the collapse param with the delimiter. And to separate the elements from multiple lists use sep param. Let’s create a list by using a combined function list().

1.1 Syntax


# paste() syntax
paste(…, sep = " ", collapse = NULL)

... – one or more input objects to merge.
sep – character to separate the values between multiple vectors. In order to use sep param, you should use multiple vectors as input.
collapse – character to separate values within a vector

1.2 Example

The following example convert all values from a list into a single string with the values separated by space.


# Convert List to string
li <- list('A','B','C','D','E','F')
paste(li,collapse = ' ')

# Output
[1] "A B C D E F"

2. Using toString() to Convert List to String

The toString() function can be used to convert elements from list to character string, but this function by default adds a comma separator between the elements.


# Using toString()
toString(li)

# Output
#[1] "A, B, C, D, E, F"

6. Using do.call()

Alternatively, you can also use do.call() function to use paste() function to convert list elements into a string. Use collapse to separate the string in a list by a delimiter.


# using do.call()
do.call(paste, c(li, collapse = " "))

# Output
[1] "A B C D E F"

7. stri_paste() to merge elements

Use stri_paste() function from stringi package to merge vector elements into strings in R. This function also takes collapse param to separate the strings by character. In order to use this package first you to install r package and load it by using library() function. So to convert a list to a string, I will use unlist() to convert a list to a vector and then use a vector with stri_paste(). Though using this method is not a recommended option but still wanted to cover it as this can be used when required.


# Load stringi
library('stringi')
stri_paste(unlist(li), collapse='')

8. Complete Example

Following is a complete example of converting a list to a string in R. You can download this example from Github R Programming Examples Project.


#Convert List of elements to string Examples

# Convert List to string
li <- list('A','B','C','D','E','F')
paste(li,collapse = ' ')

# Using toString()
toString(li)

# using do.call()
do.call(paste, c(li, collapse = " "))

# Load stringi
library('stringi')
stri_paste(unlist(li), collapse='')

9. Conclusion

In this article, I have explained the different ways how to convert a list of elements to a character string in R by using functions like past(), toString(), and stri_paste().

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium