[R] R help on write.csv

Jan van der Laan rhelp at eoos.dds.nl
Wed Sep 21 17:29:55 CEST 2011


Michael,

You example doesn't seem to work. Append isn't passed on to the  
write.table call. You
will need to add a

  Call$append<- append

to the function. And even then there will be a problem with the  
headers that are repeated
when appending.


An easier solution is to use write.table directly (I am using  
Dutch/European csv format):

data <- data.frame(a=1:10, b=1, c=letters[1:10])
write.table(data, file="test.csv", sep=";", dec=",", row.names=FALSE,  
col.names=TRUE)
write.table(data, file="test.csv", sep=";", dec=",", row.names=FALSE,  
col.names=FALSE,
append=TRUE)


When first openening a file connection and passing that to write.csv  
or write.table data
is also appended. The problem with write.csv is that writing the  
column names can not be
suppressed which will result in repeated column names:

con <- file("d:test2.csv", "wt")
write.csv2(data, file=con, row.names=FALSE)
write.csv2(data, file=con, row.names=FALSE)
close(con)

So one will still have to use write.table to avoid this:

con <- file("d:test2.csv", "wt")
write.table(data, file=con, sep=";", dec=",", row.names=FALSE, col.names=TRUE)
write.table(data, file=con, sep=";", dec=",", row.names=FALSE,  
col.names=FALSE,
append=TRUE)
close(con)

Using a file connection is probably also more efficient when doing a  
large number of
appends.

Jan





Quoting "R. Michael Weylandt" <michael.weylandt at gmail.com>:

> Touche -- perhaps we could make one though?
>
> write.csv.append <- function(..., append = TRUE)
> {
>     Call <- match.call(expand.dots = TRUE)
>     for (argname in c("col.names", "sep", "dec", "qmethod")) if
> (!is.null(Call[[argname]]))
>         warning(gettextf("attempt to set '%s' ignored", argname),
>             domain = NA)
>     rn <- eval.parent(Call$row.names)
>     Call$col.names <- if (is.logical(rn) && !rn)
>         TRUE
>     else NA
>     Call$sep <- ","
>     Call$dec <- "."
>     Call$qmethod <- "double"
>     Call[[1L]] <- as.name("write.table")
>     eval.parent(Call)
> }
> write.csv.append(1:5,"test.csv", append = FALSE)
> write.csv.append(1:15, "test.csv")
>
> Output seems a little sloppy, but might work for the OP.
>
> Michael Weylandt
>
> On Wed, Sep 21, 2011 at 9:03 AM, Ivan Calandra <ivan.calandra at uni-hamburg.de
>> wrote:
>
>> I don't think there is an append argument to write.csv() (well, actually
>> there is one, but set to FALSE).
>> There is however one to write.table()
>> Ivan
>>
>> Le 9/21/2011 14:54, R. Michael Weylandt <michael.weylandt at gmail.com> a
>> écrit :
>>
>>  The append argument of write.csv()?
>>>
>>> Michael
>>>
>>> On Sep 21, 2011, at 8:01 AM, "Ashish Kumar"<ashish.kumar@**
>>> esteeadvisors.com <ashish.kumar at esteeadvisors.com>>  wrote:
>>>
>>>  Hi,
>>>>
>>>>
>>>>
>>>> I wanted to write the data created using R  on existing csv file. However
>>>> everytime I use write.csv, it overwrites the values already there in the
>>>> existing csv file. Any workaround on this.
>>>>
>>>>
>>>>
>>>> Thanks for your help
>>>>
>>>>
>>>>
>>>> Ashish Kumar
>>>>
>>>>
>>>>
>>>> Estee Advisors Pvt. Ltd.
>>>>
>>>> Email: ashish.kumar at esteeadvisors.com
>>>>
>>>> Cell: +91-9654072144
>>>>
>>>> Direct: +91-124-4637-713
>>>>
>>>>
>>>>
>>>>
>>>>    [[alternative HTML version deleted]]
>>>>
>>>> ______________________________**________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/**
>>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>> --
>> Ivan CALANDRA
>> PhD Student
>> University of Hamburg
>> Biozentrum Grindel und Zoologisches Museum
>> Dept. Mammalogy
>> Martin-Luther-King-Platz 3
>> D-20146 Hamburg, GERMANY
>> +49(0)40 42838 6231
>> ivan.calandra at uni-hamburg.de
>>
>> **********
>> http://www.for771.uni-bonn.de
>> http://webapp5.rrz.uni-**hamburg.de/mammals/eng/1525_8_**1.php<http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php>
>>
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[alternative HTML version deleted]]
>
>



More information about the R-help mailing list