
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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"
Advertisements