[R] export list to csv
Ivan Calandra
ivan.calandra at uni-hamburg.de
Wed Mar 16 16:31:48 CET 2011
Hi,
You could try this (others surely have better solutions):
out_file <- file("file.csv", open="a") #creates a file in append mode
for (i in seq_along(l)){
write.table(names(l)[i], file=out_file, sep=",", dec=".",
quote=FALSE, col.names=FALSE, row.names=FALSE) #writes the name of the
list elements ("A", "B", etc)
write.table(l[[i]], file=out_file, sep=",", dec=".", quote=FALSE,
col.names=NA, row.names=TRUE) #writes the data.frames
}
close(out_file) #close connection to file.csv
HTH,
Ivan
Le 3/16/2011 16:14, andrija djurovic a écrit :
> Soryy, I didn't explain well what I want. I would like to have a table in
> csv on txt file like this:
>
> $A
> q1 q2
> aa 1 3
> bb 2 check
> cc check 5
> $B
> q1 q2
> aa check 4
> bb 1 5
> The same as write.csv of any data frame.
>
>
>
>
> On Wed, Mar 16, 2011 at 4:03 PM, Henrique Dallazuanna<wwwhsd at gmail.com>wrote:
>
>> Use dput:
>>
>> dput(l, file = "l_str.txt")
>>
>> Then, to read again:
>>
>> l<- dget(file = 'l_str.txt')
>>
>> On Wed, Mar 16, 2011 at 11:55 AM, andrija djurovic<djandrija at gmail.com>
>> wrote:
>>> Hi everybody.
>>>
>>> I have list like this:
>>>
>>> l<-list(data.frame(q1=c(1,2,"check"),q2=c(3,"check",5)),
>>> data.frame(q1=c("check",1),q2=c(4,5)))
>>> names(l)<-c("A","B")
>>> rownames(l[[1]])<-c("aa","bb","cc")
>>> rownames(l[[2]])<-c("aa","bb")
>>>
>>> Every object has the same number of columns but different number of rows.
>>> Does anyone know if it is possible to export such kind of list, into one
>> csv
>>> file, and keeping all the names?
>>>
>>> Thanks in advance
>>>
>>> Andrija
>>>
>>> [[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<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
> [[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.
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
More information about the R-help
mailing list