[R] Print list to text file with list elements names
jim holtman
jholtman at gmail.com
Wed Oct 1 14:20:38 CEST 2014
The result of the 'lapply' is also printed, so try this:
sink(sprintf("%s",filename))
invisible(lapply(myList,print))
sink()
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Oct 1, 2014 at 6:59 AM, Ingrid Charvet <Ingrid.Charvet at rms.com> wrote:
> Hi everyone,
>
> I want to write a list of data frames to a text file and preserve the names given to the list elements (I am using R 3.1.0).
>
> I tried:
>
> setNames(myList, myNames) # myNames is a vector of char elements same length as myList
>
> sink(sprintf("%s",filename))
> lapply(myList,print)
> sink()
>
> And here I have two problems:
>
> 1. R writes each element of my list to the text file twice, so for example if I have a list with 2 elements (i.e. data frames) in it, it will write 4: in order data frame 1, data frame 2, data frame 1, data frame 2.
>
> 2. The names of list elements do not print to the text file.
>
> Any suggestions to solve these issues would be appreciated!
> Many thanks,
>
> Ingrid
>
>
>
>
> ________________________________
> This message and any attachments contain information that may be RMS Inc. confidential and/or privileged. If you are not the intended recipient (or authorized to receive for the intended recipient), and have received this message in error, any use, disclosure or distribution is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to the e-mail and permanently deleting the message from your computer and/or storage system.
>
>
>
> [[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.
More information about the R-help
mailing list