[Rd] write.csv suggestion

McGehee, Robert Robert.McGehee at geodecapital.com
Wed Jun 29 22:48:29 CEST 2005


I didn't want to use a different separator, I wanted to remove
row.names, as in this example:

> data(USArrests)
> write.csv(USArrests, file = "~/test.csv", row.names = FALSE, col.names
= TRUE)
Error in if (col.names) d[[2]] else NULL : 
	missing value where TRUE/FALSE needed

I only mentioned this suggestion because the above syntax seemed
reasonable (and self-documenting), but by making the options
unchangeable, I received an unhelpful error message.

After checking the code, I rewrote the line to this:
> write.table(USArrests, file = "~/test.csv", sep = ",", row.names =
FALSE, col.names = TRUE)

This only seemed suboptimal (to me) because one would have to read the
code to know that the col.names = TRUE option was not being passed along
to write.table (as I expected).

Best,
Robert

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
Sent: Wednesday, June 29, 2005 4:16 PM
To: McGehee, Robert
Cc: r-devel at stat.math.ethz.ch
Subject: Re: [Rd] write.csv suggestion


The help page says

      By default there is no column name for a column of row names.  If
      'col.names = NA' and 'row.names = TRUE' a blank column name is
      added.  This can be used to write CSV files for input to
      spreadsheets.  'write.csv' and 'write.csv2' provide convenience
      wrappers for doing so.

and they are set up to disallow the options they set to be changed.

If you get the option wrong to read a file, you will know soon enough,
but 
these are to ensure a suitable CSV file gets written (which will not be
so
immediately apparent).

Please define `optimal': doing what it was designed for and is
documented 
to do is according to you not `optimal'.

Why would anyone want to use write.csv to write files with something
other 
than a comma/semicolon as separator, rather than use write.table?


On Wed, 29 Jun 2005, McGehee, Robert wrote:

> Hello all,
> I had some trouble recently with write.csv because I couldn't change
one
> of the default options. A quick view of the code showed that the
> function was not defined in the most optimal way.
>
> Currently,
> write.csv <- function (..., col.names = NA, sep = ",", qmethod =
> "double")
> 	write.table(..., col.names = NA, sep = ",", qmethod = "double")
>
> Thus, the options passed along to write.csv are ignored by the
function
> (unless in the ...).
>
> Perhaps a better way to define the function is as such (similar to
> read.csv):
>
> write.csv <- function (..., col.names = NA, sep = ",", qmethod =
> "double")
> 	write.table(..., col.names = col.names, sep = sep, qmethod =
> qmethod)
>
> The same also applies to write.csv2
>
> Best,
> Robert
>
>
>> version
>         _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    1.1
> year     2005
> month    06
> day      20
> language R
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

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



More information about the R-devel mailing list