[Rd] write.table with rows and cols (PR#509)

Prof Brian D Ripley ripley@stats.ox.ac.uk
Tue, 4 Apr 2000 07:25:01 +0100 (BST)


On Tue, 4 Apr 2000 edd@debian.org wrote:

> Full_Name: Dirk Eddelbuettel
> Version: 1.0.0 (patched)
> OS: Linux
> Submission from: (NULL) (192.139.71.69)
> 
> 
> A frame with rownames and colnames got properly written, but the colnames
> started 
> without an offset -- but if the .csv file is read back in, the rownames column
> now 
> has the header of the first column.  
> 
> "Graphically", suppose a frame with two rows 1999 and 2000, and two columns foo
> and 
> bar, with content equal to the row/col index.
> 
> This should be written as
> 
>   ,foo,bar
>   1999,11,12
>   2000,21,22
> 
> but was written without the initial ","  (I used "," as the sep).
> 
> Am I making sense? 

In the S convention, the header is regarded as separate, with a potentially
different format from the rest of the file. If I have

auk% cat test.csv
foo,bar
1999,11,12
2000,21,22

then

> read.table("test.csv", sep=",")
     foo bar
1999  11  12
2000  21  22

works, as does test <- read.csv("test.csv"). If I then do

> write.table(test, "test2.csv", sep=",", quote=F)

I get

auk% cat test2.csv
foo,bar
1999,11,12
2000,21,22

as before, so it seems that this is working as expected.  Can you give the
actual example you used that fails?  There are a lot of other things which
can potentially go wrong, including the quote setting (and I had to use
quote=F to approximate the file you quote).

Note that when I read your suggested correction with read.table/read.cvs I
get

    NA foo bar
1 1999  11  12
2 2000  21  22

unless I use read.csv("test2.csv", row.names=1).


-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._