Skip to content

Wriritng to a CSV file

6 messages · Ron Michael, Uwe Ligges, David Winsemius +1 more

#
Dear all, let say I want to write a vector to a CSV file. So I can have following syntax:
?
write.csv(rnorm(10), "dat.csv")
?
Now I want to add one more column into that existing file. If I use the same code then existing file will be destroyed. Is there any functionality to add without destroying the existing file? I have tries with 'append = TRUE' however it is not working!
?
Any suggestion will be highly appreciated.
#
On 08.02.2012 20:14, Ron Michael wrote:
Since these text files are written line by line, you cannot add a column 
afterwards, you can just append rows.

Hence read the first column and write a two column data.frame afterwards.

Uwe Ligges
#
Okay, so I understood that appending can only happen row-wise. Therefore I tried with following code:
?
Warning message:
In write.csv(matrix(1:5, 1), "dat.csv", append = TRUE) :
? attempt to set 'append' ignored

It is destroying my previous file. Where I have done wrong?
?
Thanks,


----- Original Message -----
From: Uwe Ligges <ligges at statistik.tu-dortmund.de>
To: Ron Michael <ron_michael70 at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Thursday, 9 February 2012 1:07 AM
Subject: Re: [R] Wriritng to a CSV file
On 08.02.2012 20:14, Ron Michael wrote:
Since these text files are written line by line, you cannot add a column 
afterwards, you can just append rows.

Hence read the first column and write a two column data.frame afterwards.

Uwe Ligges
#
I (and you as well) should have seen that before: use write.table in 
order to append. The reason for that is given in ?write.table / ?write.csv:

      ?write.csv? and ?write.csv2? provide convenience wrappers for
      writing CSV files.  They set ?sep? and ?dec? (see below), ?qmethod
      = "double"?, and ?col.names? to ?NA? if ?row.names = TRUE? (the
      default) and to ?TRUE? otherwise.

      ?write.csv? uses ?"."? for the decimal point and a comma for the
      separator.

      ?write.csv2? uses a comma for the decimal point and a semicolon
      for the separator, the Excel convention for CSV files in some
      Western European locales.

      These wrappers are deliberately inflexible: they are designed to
      ensure that the correct conventions are used to write a valid
      file.  Attempts to change ?append?, ?col.names?, ?sep?, ?dec? or
      ?qmethod? are ignored, with a warning.


Uwe Ligges
On 08.02.2012 20:29, Ron Michael wrote:
#
On Feb 8, 2012, at 2:29 PM, Ron Michael wrote:

            
Failed to read the help page. `write.csv` has some of its setting hard  
coded and will prevent you from changing them. "append" happened to be  
in that list.

Here's the code.... it's right there for all to see:

     Call <- match.call(expand.dots = TRUE)
     for (argname in c("append", "col.names", "sep", "dec",  
"qmethod")) if (!is.null(Call[[argname]]))
         warning(gettextf("attempt to set '%s' ignored", argname),
             domain = NA)
#
On Feb 8, 2012, at 21:56 , David Winsemius wrote:

            
Exactly. Note, however, that write.csv is really just write.table with a particular set of arguments. Nothing is keeping you from using a similar set of arguments with append=TRUE in an explicit write.table() call.

It is of course debatable whether the behavior of write.csv is undue patronizing, but as I understand it, the rationale is that you can guarantee that write.csv creates a proper CSV file, but once you start appending, multiple things can go wrong: Forgetting to omit headers, different number of columns, different column types, etc. It is not realistic to have write.csv(...append=TRUE) make the necessary checks, and as it can't be sure to write a properly formed CSV, it just won't do it, period.