Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid splitting string columns when writing csv

Tags:

r

csv

In a dataset, I have a few columns where the contents are names or addresses that may contain commas. For example, "Einstein, Albert" or "Devon St., 8". When trying to write a csv file using the write.csv command, R splits the strings and creates additional columns in some cases:

write.csv(data, "output.csv", rownames=F, quote=F)

Name          Address          NumberP           Phone
Einstein      Albert           Rue 8             8             00000000000
David Rosa    Ocho 9           11                0000000000000

How can I bypass this issue?

like image 224
Tamara Dominguez Poncelas Avatar asked Dec 24 '22 09:12

Tamara Dominguez Poncelas


1 Answers

The quote=F parameter you passed in your call to write.csv might tip it off for you. You told R to not quote the fields, which means that a column with literal commas will therefore appear in the output with literal commas. Changing to quote=TRUE should get around this problem:

write.csv(data, "output.csv", rownames=FALSE, quote=TRUE)

Note that now your output will have each field escaped in double quotes, at least those which need it to be unambiguous. But most place where you would import this CSV file know how to handle this (e.g. Excel).

like image 183
Tim Biegeleisen Avatar answered Jan 14 '23 11:01

Tim Biegeleisen