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?
Sometimes column values in an R data frame have single quote associated with them and to perform the analysis we need to remove that quote. Therefore, to remove single quote from string column, we can use gsub function by defining the single quote and replacing it with blank(not space) as shown in the below examples.
Since CSV files use the comma character "," to separate columns, values that contain commas must be handled as a special case. These fields are wrapped within double quotation marks. The first double quote signifies the beginning of the column data, and the last double quote marks the end.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With