Will T-E Will T-E - 18 days ago 7
R Question

How do I write a csv from R without quoted values?

I'm looking to create a csv from R where none of the values have quotes around them. This seems to be the norm when you create a CSV in excel but not in R.

Take the following example:

set.seed(100)
df <- data.frame(Apples=sample(1:10, 5), Oranges=sample(1:10, 5), Bananas=sample(1:10, 5), Dates=sample(1:10, 5))
write.table(df, sep=",", row.names = FALSE)


If you open it in notepad you see the column headers have quotes around them. I need it to be without quotes if the csv is to be successfully imported into another program.

I noticed that re-saving the csv in Excel causes the quotes to disappear but I need to avoid having to make this step.

I tried things like
col.names = noquote(colnames(LineupCSV))
as an argument in
write.table
but without success.

How can I remove the quotes around values when I write a csv in R?

Answer

If you want to write a data frame as a csv file without quoting values and strings then you can set the quote=FALSE argument when calling write.table or one if its wrappers such as write.csv and write.csv2.

set.seed(100)
df <- data.frame(Apples=sample(1:10, 5), 
                 Oranges=sample(1:10, 5), 
                 Bananas=sample(1:10, 5), 
                 Dates=sample(1:10, 5))
write.table(df, file="~/test.csv", sep=",", row.names = FALSE, quote=FALSE)

Alternatively, you can use fwrite from the data.table package (at least from 1.97 onwards)

fwrite(df, file="~/test2.csv")
Comments