carljong2019
carljong2019

Reputation: 107

R write.csv not handling characters like é correctly

When I look at data in R, it has characters like "é" displayed correctly.

I export it to excel using write.csv. When I open the csv file, "é" is displayed as "√©". Is the problem with write.csv or with excel? What can I do to fix it?

Thanks

Upvotes: 5

Views: 9304

Answers (3)

wanderzen
wanderzen

Reputation: 119

Try the write_excel_csv() function from the readr package

readr::write_excel_csv(your_dataframe, "file_path")

Upvotes: 8

Wilfredo
Wilfredo

Reputation: 55

It's a problem with Excel. Try Importing data instead of Opening the file.

Go to: 'Data' --> 'From Text/CSV' and then select '65001:Unicode (UTF-8)'. That will match the encoding from R.

enter image description here

enter image description here

Upvotes: 3

R A
R A

Reputation: 36

Try experimenting with the parameter fileEncoding of write.csv:

write.csv(..., fileEncoding="UTF-16LE")

From write.csv documentation:

fileEncoding character string: if non-empty declares the encoding to be used on a file (not a connection) so the character data can be re-encoded as they are written. See file.

CSV files do not record an encoding, and this causes problems if they are not ASCII for many other applications. Windows Excel 2007/10 will open files (e.g., by the file association mechanism) correctly if they are ASCII or UTF-16 (use fileEncoding = "UTF-16LE") or perhaps in the current Windows codepage (e.g., "CP1252"), but the ‘Text Import Wizard’ (from the ‘Data’ tab) allows far more choice of encodings. Excel:mac 2004/8 can import only ‘Macintosh’ (which seems to mean Mac Roman), ‘Windows’ (perhaps Latin-1) and ‘PC-8’ files. OpenOffice 3.x asks for the character set when opening the file.

Upvotes: 1

Related Questions