Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

csv

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?

like image 555
Will T-E Avatar asked Nov 14 '16 18:11

Will T-E


People also ask

How do I remove quote marks from data 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.

Why does my csv have double quotes?

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.


1 Answers

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")
like image 140
ekstroem Avatar answered Sep 26 '22 17:09

ekstroem