如果函数源于stringr,则Stringi/stringr模式的行为不同
我现在正在使用如果函数源于stringr,则Stringi/stringr模式的行为不同,r,stringr,stringi,R,Stringr,Stringi,我现在正在使用stringi软件包一段时间,一切正常 我最近想在一个函数中放入一些正则表达式,并将该函数存储在一个单独的文件中。如果函数是从脚本中加载的,那么代码就可以正常工作,但是当它是源代码时,我没有得到预期的结果 以下是重现问题的代码: clean <- function(text){ stri_replace_all_regex(str = text, pattern = "(?i)[^a-zàâçéèêëîïôûùüÿñæœ
stringi
软件包一段时间,一切正常
我最近想在一个函数中放入一些正则表达式,并将该函数存储在一个单独的文件中。如果函数是从脚本中加载的,那么代码就可以正常工作,但是当它是源代码时,我没有得到预期的结果
以下是重现问题的代码:
clean <- function(text){
stri_replace_all_regex(str = text,
pattern = "(?i)[^a-zàâçéèêëîïôûùüÿñæœ0-9,\\.\\?!']",
replacement = " ")
}
text <- "A sample text with some french accent é, è, â, û and some special characters |, [, ( that needs to be cleaned."
clean(text) # OK
[1] "A sample text with some french accent é, è, â, û and some special characters , , that needs to be cleaned."
source(clean.r)
clean(text) # KO
[1] "A sample text with some french accent , , , and some special characters , , that needs to be cleaned."
首先尝试将文本转换为ASCII。这将更改字符,并且在R中生成函数时可能允许相同的行为 +1给Felipe Alvarenga
text我也有类似的问题。我认为这与文件来源时如何读取特殊字符有关。我的文本包含“;”而你的文本包含法语重音“é,è,è”。
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C
[5] LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringi_1.1.5 data.table_1.10.4
loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1 yaml_2.1.14
text <- "Ábcdêãçoàúü"
iconv(text, to = "ASCII//TRANSLIT")