alperovich
alperovich

Reputation: 884

Cyrillic encoding output in R

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,

Upvotes: 5

Views: 13611

Answers (2)

baddog
baddog

Reputation: 480

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

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

Upvotes: 2

Mustafa
Mustafa

Reputation: 87

If you just visit the help page of Encoding() you find the native function enc2native(x), this'll do the trick as in

test = enc2utf8(c("привет","пока"))

Upvotes: 2

Related Questions