可能是Unicode问题。在R(studio)中,不是所有字符都被创建得一样。
我试图创建一个可重现的例子:
# create the ellipsis from the definition (similar to your tags_used)
> ell_def <- rawToChar(as.raw(c('0xE2','0x80','0xA6'))) # from the unicode definition here: http://www.fileformat.info/info/unicode/char/2026/index.htm
> Encoding(ell_def) <- 'UTF-8'
> ell_def
[1] "…"
> Encoding(ell_def)
[1] "UTF-8"
# create the ellipsis from text (similar to your string)
> ell_text <- '…'
> ell_text
[1] "…"
> Encoding(ell_text)
[1] "latin1"
# show that you can get strange results
> gsub(ell_text,'',ell_def)
[1] "…"
这个例子的可重复性可能取决于您的区域设置。在我的情况下,我使用 windows-1252,因为在 Windows 中无法将区域设置设置为 UTF-8。根据
this stringi source,“R 可以让 ASCII、UTF-8 和您平台的本地编码的字符串和平共处”。就像上面的例子所示,有时这可能会导致矛盾的结果。
基本上,您看到的输出看起来相同,但在字节级别上却不同。
如果我在 R 终端中运行此示例,则会获得类似的结果,但显然它将省略号显示为点号:“。”。
您的示例的快速修复方法是在 gsub 中使用省略号的定义。例如:
gsub(ell_def,'',tags_vector[5])
tags_vector
吗?我用一个简单的x <- "#b..."
可以运行,所以我猜测问题出在你的向量上。 - LAPstr_replace_all(tags_vector[5], "…", "")
- Wiktor Stribiżewgsub(gsub("[#b]","",tags_vector[5]), "", tags_vector[5])
吗? - takje