how to write csv in r where data is cleaned with UTF-8 characters?

Initial csv file is having Thai Language in some columns, i have queried the data from SQL server and save as csv-utf8 format, so csv itself having thai language, while loading in r,i set below code and done cleaning,

Sys.setlocale(locale="Thai")
data <- read.csv("data.csv",encoding = "UTF-8")

#Successfully read the data in Thai in R, after cleaning the data, for write.csv i gave

write.csv(data,"data.csv",fileEncoding = "UTF-8")

#But this is not writing back in Thai Language in csv,
#I have tried

encodeConnection <- file('data.csv', encoding = "UTF-8")
write.csv(data, file = encodeConnection)
}
#but not working
#There is no error message, csv is created, but those columns were not in thai, there are special characters like หมุนเวียนในà¸à¸´à¸ˆà¸à¸²à¸£, 
# can someone help on this please, Thanks in advance

This is a known issue with the read/write functions from the standard R packages. In R's defense, character encoding is a headache in most programming languages.

I'd suggest using readr::write_csv() or data.table::fwrite(). They preserve encoding.

5 Likes

Thanks for that, I have tried that, but no luck,

readr::write_csv(data,"data.csv")
data.table::fwrite(data,"data.csv")

csv got these characters in place of Thai
บัตรประชาชน

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.