Search code examples
rencodingutf-8file-encodings

Write as CSV giving weird characters


I am trying to write a dataframe as csv, which succeeds, but end up some fields with weird characters, Say for example 4.5×10−7 gives 4.5×10−7 in the csv file. After doing few research I used fileEncoding as "Windows-1252", but that dint help. Here is a reproducible code

name <- c('John Doe','Peter Gynn','Jolie Hope')
valuename <- c("4.5×10−7", "0.0006", "0.345")
df <- data.frame(name,valuename)
write.csv(df, "/Desktop/test.csv", row.names=FALSE)

Can anyone help me with a proper encoding or an alternative to take care of that field?


Solution

  • Try to force the fileEncoding to use "UTF-8":

    write.csv(df, "/Desktop/test.csv", row.names=FALSE, fileEncoding = 'UTF-8')

    Your code works fine in my windows 7, but I already had similar problems on my ubuntu 16.04.