Skip to content

how do I exort a list of numbers into csv file?

5 messages · Michael, Joshua Wiley, Baptiste Auguie +2 more

#
Hi Michael,

Part of me imagines this is overkill, but this should be one option:

## your data
mylist <- list(1:3, 3:6)
## open a writeable connection to a file
con <- file("test.csv", "w")
## first collapse each element of the list to be a comma separated
string, then write each
## element of new character vector to con using writeLines
writeLines(sapply(mylist, paste, collapse = ", "), con = con)
## close the connection
close(con)

see ?writeLines for details on different ways to indicate end of the line.

Hope this helps,

Josh
On Sun, Feb 5, 2012 at 6:01 PM, Michael <comtech.usa at gmail.com> wrote:

  
    
#
Hi,

If you don't mind having NAs for missing values, try the following,

mylist = list(1:3, 4:7)
library(plyr)
write.csv(do.call(rbind.fill.matrix, lapply(mylist, matrix, nrow=1)), file="")

HTH,

b.
On 6 February 2012 15:01, Michael <comtech.usa at gmail.com> wrote:
#
michael,

what is the problem you are trying to solve?  are you writing it out so you can read it back in?  if  so, look at 'save/load' or 'dump/source'.  is this the format required by some other program?

Sent from my iPad
On Feb 5, 2012, at 21:54, baptiste auguie <baptiste.auguie at googlemail.com> wrote:

            
#
cat will wrap Josh's "overkill" approach in one line:

mylist<- list(1:3,3:9)
lapply(mylist , cat , sep=',' , fill=T , append=T , file='foo.csv')

Cheers
On Sun, Feb 5, 2012 at 7:42 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote: