[R] Vectorize 'eol' characters
dwinsemius at comcast.net
Tue Nov 1 02:05:47 CET 2011
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?)
> Dr Stefano Conti
> Statistics Unit (room #2A19)
> Health Protection Services
> HPA Colindale
> 61 Colindale Avenue
> London NW9 5EQ, UK
> tel: +44 (0)208-3277825
> fax: +44 (0)208-2007868
> -----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
>> 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.
>> With many thanks in advance for your help, kind regards,
>> Dr Stefano Conti
>> Statistics Unit (room #2A19)
>> Health Protection Services
>> HPA Colindale
>> 61 Colindale Avenue
>> London NW9 5EQ, UK
>> tel: +44 (0)208-3277825
>> fax: +44 (0)208-2007868
>> The information contained in the EMail and any attachments is
>> confidential and intended solely and for the attention and use of
>> the named addressee(s). It may not be disclosed to any other person
>> without the express authority of the HPA, or the intended
>> recipient, or both. If you are not the intended recipient, you must
>> not disclose, copy, distribute or retain this message or any part
>> of it. This footnote also confirms that this EMail has been swept
>> for computer viruses, but please re-sweep any attachments before
>> opening or saving. HTTP://www.HPA.org.uk
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
> R-help at r-project.org mailing list
> 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