[R] R help on write.csv

Jan van der Laan eoos at dds.nl
Thu Sep 22 06:56:31 CEST 2011


Rowwise is easy. The example code I gave does this: it appends the new 
data /below/ the old. I'll repeat the example below:

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)

Or do you mean columnwise where you append columns? This would be very 
difficult in CSV. If you would like to do this you might have a look at 
the various options for exporting to Excel directly.  See for example 
http://rwiki.sciviews.org/doku.php?id=tips:data-io:ms_windows . I have 
no experience in this.

Regards,
Jan

PS I am sorry for my previous triple post. I had a little fight with my 
webmail client.


On 09/22/2011 06:14 AM, Ashish Kumar wrote:
>
> IS there a way we can append row wise, so that it all stacks up 
> horizontally, the way you do it in xlswrite in matlab, where you can 
> even specify the cell number from where you want to write.
>
> -Ashish
>
> *From:*R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
> *Sent:* Thursday, September 22, 2011 12:03 AM
> *To:* Jan van der Laan
> *Cc:* r-help at r-project.org; ashish.kumar at esteeadvisors.com
> *Subject:* Re: [R] R help on write.csv
>
> Oh darn, I had that line and then when I copied it to gmail I thought 
> I'd be all slick and clean up my code: oh well...just not my day/thread...
>
> It's possible to work around the repeated headers business (change to 
> something like "Call$col.names <- !append") but yeah, at this point 
> I'm thinking its perhaps better practice to direct the OP to the 
> various connection methods: sink() is nice, but he'll probably have to 
> do something to convert his object to a CSV like string before printing:
>
> apply(OBJ, 1, paste, sep=",")
>
> Michael Weylandt
>
> On Wed, Sep 21, 2011 at 11:20 AM, Jan van der Laan <eoos at dds.nl 
> <mailto:eoos at dds.nl>> wrote:
>
> 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 
> <mailto: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 <http://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 <mailto: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 <mailto: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 <http://esteeadvisors.com>
>             <ashish.kumar at esteeadvisors.com
>             <mailto: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
>             <mailto:ashish.kumar at esteeadvisors.com>
>
>             Cell: +91-9654072144 <tel:%2B91-9654072144>
>
>             Direct: +91-124-4637-713 <tel:%2B91-124-4637-713>
>
>
>
>
>               [[alternative HTML version deleted]]
>
>             ______________________________**________________
>             R-help at r-project.org <mailto: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 <mailto: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 <tel:%2B49%280%2940%2042838%206231>
>         ivan.calandra at uni-hamburg.de <mailto: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://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 <mailto: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]]
>
>
> ______________________________________________
> R-help at r-project.org <mailto:R-help at r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list