Cyrillic encoding output in R

做~自己de王妃 提交于 2019-11-27 02:58:22

问题


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,


回答1:


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

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




回答2:


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("привет","пока"))


来源:https://stackoverflow.com/questions/14691555/cyrillic-encoding-output-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!