R write.csv not handling characters like é correctly
Asked Answered
L

3

5

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

Longoria answered 5/9, 2020 at 14:16 Comment(1)
perhaps try to use the encoding="UTF-8" argumentSelfindulgent
D
8

Try the write_excel_csv() function from the readr package

readr::write_excel_csv(your_dataframe, "file_path")
Disunity answered 11/7, 2022 at 14:21 Comment(1)
That works perfectly! Thanks!Ladyship
I
3

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

Incorporating answered 5/9, 2020 at 14:40 Comment(0)
Z
1

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.

Zamir answered 5/9, 2020 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.