[R] Writing a list to a file !

Michael Prager Mike.Prager at noaa.gov
Tue Dec 6 21:14:38 CET 2005

Here is a function I use to send lists to ASCII files.

list2ascii <- function(x,file=paste(deparse(substitute(x)),".txt",sep=""))
   # MHP July 7, 2004
   # R or S function to write an R list to an ASCII file.
   # This can be used to create files for those who want to use
   # a spreadsheet or other program on the data.
   tmp.wid = getOption("width")  # save current width
   options(width=10000)          # increase output width
   sink(file)                    # redirect output to file
   print(x)                      # print the object
   sink()                        # cancel redirection
   options(width=tmp.wid)        # restore linewidth
   return(invisible(NULL))       # return (nothing) from function

I hope it's helpful.

To write it to a file that can be read by R, I would suggest using 
"dput" instead.

Mike Prager

A Ezhil wrote:

>Hi All,
>This may be trivial in R but I have been trying with
>out any success. I have a list of 100 elements each
>having a sub list of different length. I would like to
>write the list to a ASCII file. I tried with
>write.table(), after converting my list to a matrix.
>Now it looks like
>Robert  c("90", "50", "30")
>John    c("91", "20", "25", "45")
>How can I get rid off c("", ..)? In my file, I would
>like to have 
>Robert  90, 50, 30
>John    91, 20, 25, 45
>Thanks in advance.
>R-help at stat.math.ethz.ch mailing list
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list