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 is considered 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 divide string elements within a single list, utilize the collapse parameter along with the specified delimiter. For separating elements across multiple lists, use the sep parameter.

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. 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 transforms a list of values into a single string, with each value separated by a 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"

3. By paste() Function

Similarly, you can use the do.call() function along with the paste() function to convert list elements into a single string. Use the collapse parameter to specify the delimiter that separates the elements in the resulting string.


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

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

4. stri_paste() to merge elements

To merge vector elements into strings in R using the stri_paste() function from the stringi package, you will also utilize the collapse parameter to specify the character to separate the strings. To begin, you will need to install the stringi package using the install.packages() function and then load it using the 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='')

5. 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='')

6. 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