Alex M
Alex M

Reputation: 125

Export Cyrillic characters from R?

I have a dataset where one of the columns includes Russian words:

raw_data2 = structure(list(word = c("абрикос", 
                                    "автомобиль", 
                                    "аист", 
                                    "ананас", 
                                    "апрель", 
                                    "атака", 
                                    "баклажан"), 
subject_nr = c(3L, 21L, 12L, 17L, 8L, 1L, 17L), 
acc = c(98.976109215, 91.8803418803, 94.8979591837, 94.5273631841, 94.4444444444, 94.5355191257, 94.3661971831)), 
row.names = c(1L, 100L, 200L, 300L, 400L, 500L, 600L), 
class = "data.frame")

When I look at the file in RStudio there's no problem:

enter image description here

However, when I export the data into a table to work with them further in Excel I get this UTF-mess which Excel cannot convert back into Russian words (even when UTF-8 is chosen during data importing):

"word";"subject_nr";"acc"
"<U+0430><U+0431><U+0440><U+0438><U+043A><U+043E><U+0441>";3;98,976109215
"<U+0430><U+0432><U+0442><U+043E><U+043C><U+043E><U+0431><U+0438><U+043B><U+044C>";21;91,8803418803
"<U+0430><U+0438><U+0441><U+0442>";12;94,8979591837
"<U+0430><U+043D><U+0430><U+043D><U+0430><U+0441>";17;94,5273631841
"<U+0430><U+043F><U+0440><U+0435><U+043B><U+044C>";8;94,4444444444
"<U+0430><U+0442><U+0430><U+043A><U+0430>";1;94,5355191257
"<U+0431><U+0430><U+043A><U+043B><U+0430><U+0436><U+0430><U+043D>";17;94,3661971831

Is there any way to force R to replace those strings with corresponding Cyrillic letters when saving the table? It certainly "knows" what these letters are, since it shows them in preview. I use the following code (which does not work):

write.table(raw_data2,
            file = "raw_data2.csv",
            append = FALSE,
            quote = TRUE,
            sep = ";",
            eol = "\n",
            na = "NA",
            dec = ",",
            row.names = FALSE,
            col.names = TRUE,
            qmethod = c("escape", "double"),
            fileEncoding = "UTF-8")

Upvotes: 1

Views: 1012

Answers (2)

KuzmaKuzmi4
KuzmaKuzmi4

Reputation: 1

For me, Sys.setlocale("LC_CTYPE", "russian") works well (code source: https://www.r-bloggers.com/2013/01/r-and-foreign-characters/)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389355

Works fine for me if you write it to xlsx file.

openxlsx::write.xlsx(raw_data2, 'temp.xlsx')

Upvotes: 2

Related Questions