writeCSV: Write CSV files with quotes same as MS Excel 2013 or newer

View source: R/utils.R

writeCSVR Documentation

Write CSV files with quotes same as MS Excel 2013 or newer

Description

R's write.csv inserts quotes around all elements in a character vector (if quote = TRUE). In contrast, MS Excel CSV export no longer inserts quotation marks on all elements in character variables, except when the cells include commas or quotation marks. This function generates CSV files that are, so far as we know, exactly the same "quoted style" as MS Excel CSV export files.

Usage

writeCSV(x, file, row.names = FALSE)

Arguments

x

a data frame

file

character string for file name

row.names

Default FALSE for row.names

Details

This works by manually inserting quotation marks where necessary and turning FALSE R's own method to insert quotation marks.

Value

the return from write.table, using revised quotes

Author(s)

Paul Johnson

Examples

set.seed(234)
x1 <- data.frame(x1 = c("a", "b,c", "b", "The \"Washington, DC\""),
      x2 = rnorm(4), stringsAsFactors = FALSE)
x1
fn <- tempfile(pattern = "testcsv", fileext = ".csv")
writeCSV(x1, file = fn)
readLines(fn)
x2 <- read.table(fn, sep = ",", header = TRUE, stringsAsFactors = FALSE)
all.equal(x1,x2)

kutils documentation built on Sept. 17, 2023, 5:06 p.m.