[R] How to write '"' to a csv with the default setting ofwrite.csv?

William Dunlap wdunlap at tibco.com
Fri Jan 22 02:00:32 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Peng Yu
> Sent: Thursday, January 21, 2010 3:38 PM
> To: milton ruser
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to write '"' to a csv with the default 
> setting ofwrite.csv?
> 
> On Thu, Jan 21, 2010 at 5:32 PM, milton ruser 
> <milton.ruser at gmail.com> wrote:
> > Hi Peng,
> >
> > May be write.table(...,quote=F)
> >
> > bests
> >
> > milton
> 
> Would this cause some trouble when filed is missing, etc.? Although I
> don't have a concrete example, I think that it might happen. So I
> prefer to have quote=T.
> 
> I have looking for a way so that the resulted csv file can be 
> read by excel.
> 
> http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat

The doubled double quote seems to be Excel's way of
escaping the usual meaning of a double quote in a csv
file.  There is nothing like doing a quick test:
   > x<-rbind('"A"', '"DoubleQuote', NA_character_, '', "'Apostrophe", 'F\tTabbed')
   > x<-cbind(state.abb[31:36],x,sub(" ", ", ", state.name[31:36]))
   > write.csv(x, "e:/temp/test.csv", row.names=FALSE)
   > cat(readLines("e:/temp/test.csv"), sep="\n")
   "V1","V2","V3"
   "NM","""A""","New, Mexico"
   "NY","""DoubleQuote","New, York"
   "NC",NA,"North, Carolina"
   "ND","","North, Dakota"
   "OH","'Apostrophe","Ohio"
   "OK","F Tabbed","Oklahoma"

I looked at this with MS Excel 2007.
Excel printed a funny glyph for the
tab but everything else looked as one
would hope.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> 
> > On Thu, Jan 21, 2010 at 6:29 PM, Peng Yu 
> <pengyu.ut at gmail.com> wrote:
> >>
> >> Please see the following example. I can not write '"' to a csv file
> >> successfully. Could somebody let me if it is possible to 
> write '"' to
> >> a csv file with the default setting of write.csv?
> >>
> >> my_home$ Rscript main_quote.R
> >> > x=rbind(
> >> +   "\"A\""
> >> +   , "\"B\""
> >> +   )
> >> > x
> >>     [,1]
> >> [1,] "\"A\""
> >> [2,] "\"B\""
> >> >
> >> > write.csv(x, "main_quote.csv", row.names=F)
> >> >
> >> my_home$ cat main_quote.csv
> >> "V1"
> >> """A"""
> >> """B"""
> >>
> >> ______________________________________________
> >> 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.
> >
> >
> 
> ______________________________________________
> 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