- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to convert list elements into a single string in R?

To convert list elements into a single string, we can use paste function but firstly we would need to unlist the elements. Also, since we want to create a single string double quotes will be required at the end of the output. For example, if we have a list that contains 5 elements say 1, 2, 3, 4, 5 then conversion of these elements into a single string would be "12345".

## Example1

List1<−list(1,2,3,2,2,1,2,1,4,2,2,2,2,2,2,3,2,2) List1

## Output

[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 2 [[5]] [1] 2 [[6]] [1] 1 [[7]] [1] 2 [[8]] [1] 1 [[9]] [1] 4 [[10]] [1] 2 [[11]] [1] 2 [[12]] [1] 2 [[13]] [1] 2 [[14]] [1] 2 [[15]] [1] 2 [[16]] [1] 3 [[17]] [1] 2 [[18]] [1] 2

Converting List1 elements into a single string element −

## Example

List1<−paste(unlist(List1),collapse="") List1

## Output

[1] "123221214222222322"

## Example

class(List1)

## Output

[1] "character"

## Example2

List2<−list(x1<−rpois(5,5),x2=rpois(5,2),x3=rpois(5,5),x4=rpois(10,4)) List2

## Output

[[1]] [1] 6 2 8 3 5 $x2 [1] 2 4 3 2 5 $x3 [1] 8 1 12 1 4 $x4 [1] 7 5 4 3 6 2 5 3 2 2

Converting List2 elements into a single string element −

## Example

List2<−paste(unlist(List2),collapse="") List2

## Output

[1] "62835243258112147543625322"

- Related Questions & Answers
- How to convert a list of lists into a single list in R?
- How to convert a list into an array in R?
- How to convert matrix rows into a list in R?
- How to convert a matrix column into list in R?
- How to convert a matrix into a matrix with single column in R?
- How to convert a string vector into title case in R?
- Convert a single column matrix into a diagonal matrix in R.
- Convert a string representation of list into list in Python
- How to convert a list of characters into a string in C#?
- How to convert matrices stored in a list into vectors in R?
- How to convert columns of an R data frame into a single vector?
- How to combine vectors of equal length into a list with corresponding elements representing a single element of the list in R?
- How to convert a string vector into an integer vector in R?
- Convert a list of multiple integers into a single integer in Python
- How to convert diagonal elements of a matrix in R into missing values?

Advertisements