Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyrillic encoding output in R

Tags:

output

r

encoding

Encoding is always a pain for me, and again it's impossible to write file with russian text. What should I do for this?

 >test = c("привет","пока")
 >test
 [1] "\320\277\321\200\320\270\320\262\320\265\321\202" "\320\277\320\276\320\272\320\260"

 >Encoding(test)
 [1] "unknown" "unknown"

 > f = file("test.txt", encoding = "UTF-8")

 > write(t,f)
 Error in cat(list(...), file, sep, fill, labels, append) : 
 argument 1 (type 'closure') cannot be handled by 'cat'

 > Encoding(test) = "UTF-8"
 > test
 [1] "<U+043F><U+0440><U+0438><U+0432><U+0435><U+0442>" "<U+043F><U+043E><U+043A><U+0430>"  

 > write(t,f)
 Error in cat(list(...), file, sep, fill, labels, append) : 
 argument 1 (type 'closure') cannot be handled by 'cat'  

I use R-studio 0.97.312, Mac OS 10.7.5,

like image 288
alperovich Avatar asked Oct 05 '22 03:10

alperovich


1 Answers

I know your pain about encoding troubles:( Hope this will help you:

    > Sys.setlocale(,"ru_RU")
    [1] "ru_RU/ru_RU/ru_RU/C/ru_RU/C"
    > test = c("привет","пока")
    > write(test, file="test.txt")

You can even use cyrillic variables after that Sys.setlocale(,"ru_RU"):

   > привет <- rnorm(100)
   > min(привет)
   [1] -2.54578

Так что удачи! :)

like image 120
iezepov Avatar answered Oct 13 '22 09:10

iezepov