在R中对于区域设置感到困惑

7

刚才我回答了这个在R中删除欧元符号后的字符问题。但是对于我来说它没有生效,而对于其他使用Ubuntu的人来说却有效。

这是我的代码:

x <- "services as defined in this SOW at a price of € 15,896.80 (if executed fro"
euro <- "\u20AC"
gsub(paste(euro , "(\\S+)|."), "\\1", x)
# "" 

我认为这与更改本地设置有关,但我不知道如何做到这一点。

我在Windows 8上运行rstudio。

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] tools_3.2.0

@Anada的回答很好,但是我们需要补充说明每次在正则表达式中使用Unicode时都需要添加encoding参数。有没有办法在Windows上修改默认编码为utf-8


如果您不设置默认编码,则文件将使用UTF-8(在Mac桌面,Linux桌面和服务器上)或系统的默认编码(在Windows上)打开。 - Wiktor Stribiżew
您可以使用 options(encoding = "UTF-8") 来设置默认编码。但这并不是您想要的魔法解决方案。在 Windows 上,R 对 UTF-8 的支持并不好;我还记得有关它需要大量转换为和从 UTF-16 的对话,这会导致一些问题。 - Richie Cotton
1个回答

6

似乎存在编码问题。

请考虑以下内容:

x <- "services as defined in this SOW at a price of € 15,896.80 (if executed fro"
gsub(paste(euro , "(\\S+)|."), "\\1", x)
# [1] ""
gsub(paste(euro , "(\\S+)|."), "\\1", `Encoding<-`(x, "UTF8"))
# [1] "15,896.80"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接