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

ilai keren at math.montana.edu
Mon Feb 6 06:44:18 CET 2012


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:
> 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 all,
>>
>> I have a list of vector of numbers - the reason I used list of vector was
>> that I each list have different numbers of numbers which I don't know
>> before run-time.
>>
>> mylist[[1]]  = c(1, 2, 3)
>> mylist[[2]] = c (3, 4, 5, 6)
>> ...
>> ...
>> etc.
>>
>> Could you please tell me if there is a way to dump all these at once into
>> csv file such that each row correspond to a vector or a cell of the list as
>> shown above?
>>
>> Thanks a lot!
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list