在Rstudio中使用特殊字符 [英] Using special characters in Rstudio
问题描述
我正在使用Rstudio中的一些特殊字符.它把它们掩盖成普通的字母.
print(Safarzyńska2013")[1]"Safarzynska2013"x<-毛利人"X[1]毛利人"
有什么方法可以读取确切的原始字符.以下信息可能会有所帮助:Rstudio的默认编码为UTF-8
sessionInfo()R版本3.1.1(2014-07-10)平台:x86_64-w64-mingw32/x64(64位)语言环境:[1] LC_COLLATE =英语_美国.1252 LC_CTYPE =英语_美国.1252[3] LC_MONETARY =英语_美国.1252LC_NUMERIC = C[5] LC_TIME =英语_美国.1252附带的基本软件包:[1]统计图形grDevices utils数据集方法库通过名称空间(未附加)加载:[1] tools_3.1.1
这不是RStudio独有的问题.
在RGui控制台上键入 print(Safarzyńska2013")还将其转换为纯字母.从RGui中以UTF-8编码的脚本运行此代码将返回 [1]"Safarzy?ska2013"
.
我认为在控制台上键入此类特殊字符不是一个好主意. x<-"SomeString";Encoding(x)
返回"unknown"
,这可能是问题所在:R不知道您在控制台上使用哪种编码,并且可能没有机会获得原始编码./p>
我将Safarzyńska2013\nMāori\ n"放入以UTF-8编码的文本文件中.然后,以下方法可以正常工作:
tbl<-read.table('c:/test1.txt',编码='UTF-8',stringsAsFactors = FALSE)tbl [1,1]tbl [2,1]Encoding(tbl [1,1])#返回"UTF-8"
如果您确实要使用控制台,则可能必须掩盖特殊字符.在?Encoding
中,我们找到以下示例来创建带有特殊字符的单词:
x<--"fa \ xE7ile"编码(x)
实际上,我目前不知道如何获取特殊字符的这些代码,并且?Encoding
也没有任何提示...
I am working with some special characters in Rstudio. It coverts them into plain letters.
print("Safarzyńska2013")
[1] "Safarzynska2013"
x <- "Māori"
x
[1] "Maori"
Is there any way to read in the exact original characters. Following info might be helpful: Rstudio default encoding is UTF-8
sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.1
This not an exclusively RStudio problem.
Typing print("Safarzyńska2013")
on the console of RGui also converts them to plain letters. Running this code from an UTF-8 encoded Script in RGui returns [1] "Safarzy?ska2013"
.
I don't think that it is a good idea to type such special chars on the console. x <- "SomeString"; Encoding(x)
returns "unknown"
and that is probably the problem: R has no idea what encoding you are using on the console and probably has no chance to get your original encoding.
I put "Safarzyńska2013\nMāori\n" in a text file encoded with UTF-8. Then the following works fine:
tbl <- read.table('c:/test1.txt', encoding = 'UTF-8', stringsAsFactors = FALSE)
tbl[1,1]
tbl[2,1]
Encoding(tbl[1,1]) # returns "UTF-8"
If you really want to use the console, you probably will have to mask the special chars. In ?Encoding
we find the following example to create a word with special chars:
x <- "fa\xE7ile"
Encoding(x)
Actually I don't know at the moment how to get these codes for your special chars and ?Encoding
has also no hints...
这篇关于在Rstudio中使用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!