[R] Vectorize 'eol' characters

Stefano Conti Stefano.Conti at hpa.org.uk
Mon Oct 31 16:36:27 CET 2011


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:

> 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?  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
************


More information about the R-help mailing list