rdata.tablefwrite

How To Remove the "Â" before "£" when exporting to csv with fwrite in R


I have this code:

#set working directory
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

dt = data.table( Product = c("a","b", "c", "d", "e") , `Price in £` = c(1,2,3,4,5) )

fwrite(dt, "DTTest.csv")

but the output csv file the column called "Price in £" is now called "Price in £"

How can I get rid of the Â?


Solution

  • This depends upon what program you are using to open DTTest.csv. If you read it as plain text (for instance with Notepad) you will find £ as required. However if you are currently using Windows and opening the file with Excel you will get the result you are writing about as the file is stored with UTF8 encoding.

    There is no way to manually set encoding in fwrite(). However you may try to set bom = T:

    fwrite(dt, "DTTest.csv", bom = T)
    

    See also the help page for write.csv():

    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.

    So you may also use another function to write the table for instance:

    write.csv2(dt, "C:\\DTTest.csv", fileEncoding = "UTF-16LE")