- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 save list where each element contains equal number of values to a text file in R?
If we want to save a list to a text file then first step would be converting that list to a data frame then write.table function can be used for saving. For example, if we have a list defined as LIST and it has elements each containing 50 values then we can convert it to a data frame as −
LIST_df=as.data.frame(do.call(cbind,LIST))
Now we can save it as −
write.table(LIST_df,"LIST.txt")
Example
Consider the below list −
x1<-rnorm(20) x2<-rpois(20,10) x3<-rexp(20,1.25) List<-list(x1,x2,x3) List
Output
[[1]] [1] -0.87482685 -0.86935939 -0.17503995 0.13214020 0.84453033 0.51118627 [7] 0.57162816 -0.03326025 1.67953327 0.69913444 -1.10769165 0.49393257 [13] 0.70878293 -0.08922488 -0.03762826 1.25496797 -0.97641747 2.48736443 [19] 0.03662371 0.75675039 [[2]] [1] 10 7 10 10 8 10 9 15 7 11 12 9 15 12 2 17 13 13 16 20 [[3]] [1] 0.65789648 0.65574912 0.21113764 0.67794001 0.35300757 2.08003425 [7] 0.50181592 0.19586529 0.56956024 0.95206676 0.57016769 0.02765269 [13] 0.43137936 0.06684910 0.98390630 0.24271322 1.19059198 2.04500369 [19] 1.56828152 0.28403535
Creating a data frame of List −
Example
List.df=as.data.frame(do.call(cbind,List)) List.df
Output
V1 V2 V3 1 -0.87482685 10 0.65789648 2 -0.86935939 7 0.65574912 3 -0.17503995 10 0.21113764 4 0.13214020 10 0.67794001 5 0.84453033 8 0.35300757 6 0.51118627 10 2.08003425 7 0.57162816 9 0.50181592 8 -0.03326025 15 0.19586529 9 1.67953327 7 0.56956024 10 0.69913444 11 0.95206676 11 -1.10769165 12 0.57016769 12 0.49393257 9 0.02765269 13 0.70878293 15 0.43137936 14 -0.08922488 12 0.06684910 15 -0.03762826 2 0.98390630 16 1.25496797 17 0.24271322 17 -0.97641747 13 1.19059198 18 2.48736443 13 2.04500369 19 0.03662371 16 1.56828152 20 0.75675039 20 0.28403535
Saving the list in a text file −
Example
write.table(List.df,"List.txt")
Output
Advertisements