[R] Vectorize 'eol' characters

David Winsemius dwinsemius at comcast.net
Tue Nov 1 17:27:12 CET 2011


On Nov 1, 2011, at 5:18 AM, Stefano Conti wrote:

> Dear David,
>
> My ultimate purpose is to generate a text file encoding a LaTeX  
> table for later inclusion in a report; while I'm aware of, and  
> familiar with, Sweave, such table would feature _some_ whole or  
> partial crossing horizontal lines (\hline or \cline with varying  
> arguments, which need to be placed after the tabular end-line mark '\ 
> \'), making it amenable to neither Sweave (at least as I understand  
> it) nor similar R functions (like Frank Harrel's latex command).
>
> Hence why I'd be bothering with differentiating end-of-line  
> characters: ideally I require in my write.table statement the  
> options sep="\t&\t" and eol="\\\\\n", yet with more flexibility  
> after the LaTeX newline command '\\'.
>
> In the meantime I've managed to resolve my problem, albeit not as  
> elegantly as I'd initially wished: I've replaced the last column of  
> my original R matrix with an edited (through appropriate use of the  
> paste function) version, which now incorporates all correct end-of- 
> line strings, and then dumped to file via write.table with  
> quote=FALSE, sep="\t&\t" and eol="\n".

(Sounds like what I arrived at.)
>
> I'd be happy to stick with the above fix so long as I'm still  
> missing some better solution.  With many thanks for the pointers so  
> far, all the best,

Here is what I cobbled together to as a work-around to get rid of the  
separator before your pseudo-EOL. Seems that some parts of it might  
apply in your situation, but I'm not sure it's any better than what  
you have constructed. Perhaps it will give you further ideas about how  
to encapsulate behaviors you desire in a function:

# Take a dataframe:

dfrm <- data.frame(a=rnorm(5), b=rnorm(5), cc =paste("tt", 1:5) )

# You can remove the last separator (in this example a comma) before the
# varying "eol string" (in this example "tt") as long as it is unique  
on each line.

sub(',tt', 'tt', capture.output(write.table(dfrm , file="", sep=",",  
quote=FALSE)) )

# Then this can be written to file with:

mod.df <- sub(',tt', 'tt', capture.output(
                        write.table(dfrm , file="", sep=",",
                                     col.names=FALSE, quote=FALSE)) )
writeLines(mod.df, con=file("test.txt") )

(It will still have a regular "eol" == "\n" unless you change that it  
the writeLines call.)

-- 
David.
>
> --
> Dr Stefano Conti
>
> -----Original Message-----
> From: Comcast [mailto:dwinsemius at comcast.net]
> Sent: Tue 01/11/2011 01:05
> To: Stefano Conti
> Cc: Prof Brian Ripley; r-help at r-project.org
> Subject: Re: [R] Vectorize 'eol' characters
>
>
>
> On Oct 31, 2011, at 2:01 PM, "Stefano Conti"  
> <Stefano.Conti at hpa.org.uk> wrote:
>
>> Thanks to Dr Shepard and Prof Riply for their helpful replies.
>>
>> In my original query I should have also specified that I have tried  
>> the trick, also suggested by Prof Ripley, of appending the extra- 
>> column to the original matrix before dumping to text; however, in  
>> cases where the field separator string (argument of the 'sep'  
>> option in write.table) is non-null, I'd then have it also between  
>> the original matrix's last column and the appended text column --  
>> which is not what I want.
>>
>> Any additional suggestion / follow-up on this?  With continued  
>> thanks,
>>
>
> Sometimes it would help to answer the question, "why bother?"
>
> I can imagine and have have even tested as a concept the possibility  
> of intercepting the output of capture.output(write.table(...)) so  
> that the last separator could be removed. Before posting any code I  
> would like to see if the effort would be on target and worth the  
> further effort. What separator are you thinking would be used and  
> how complex is this rolling eol???
>
> (it's a bit like an actor saying to the director ... What's my  
> motivation?)
>
> -- 
> David.
>>
>> --
>> Dr Stefano Conti
>> Statistics Unit (room #2A19)
>>
>>
>> -----Original Message-----
>> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
>> Sent: Mon 31/10/2011 16:45
>> To: Stefano Conti
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Vectorize 'eol' characters
>>
>> On Mon, 31 Oct 2011, Stefano Conti wrote:
>>
>>> Dear R users,
>>>
>>> When dumping an R matrix object into a file -- typically via the
>>> 'write.table' function -- the 'eol' option can be used to specify
>>> the end-of-line character(s) which should appear at the end of each
>>> row.
>>>
>>> However the argument to 'eol' seems to be restricted to have length
>>> 1, whereas ideally I would like different rows to be written to file
>>> each with its own end character string.  For instance:
>>
>> That's not what 'eol' means.  It is the indicator of the end of line,
>> so of course it is the same for every line.
>>
>>>> test <- matrix(1:12, nrow=4); test
>>>   [,1] [,2] [,3]
>>> [1,]    1    5    9
>>> [2,]    2    6   10
>>> [3,]    3    7   11
>>> [4,]    4    8   12
>>>
>>>> write.table(test, file="test.txt", sep=" ", eol=paste(" test",  
>>>> 1:4, "\n", sep=""))
>>>
>>>> read.table(file="test.txt", sep=" ")
>>> V1 V2 V3 test1
>>> 1  1  5  9 test1
>>> 2  2  6 10 test1
>>> 3  3  7 11 test1
>>> 4  4  8 12 test1
>>>
>>> whereas I would like the last column of the dump file to be "test1",
>>> "test2", "test3", "test4".  Is there a way this could be achieved?
>>
>> Hmn, you said it: 'the last column'.
>> Create what you want as the last column of your data frame: it wil
>> then be written to the file as the last column.
>>
>> The author of write.table.
(B. D. Ripley.)
>>
>>> With many thanks in advance for your help, kind regards,
>>>
>>>
>>> --

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list