我习惯用trimws
摆脱文本上的任何空格。
现在我有一个用 制作的 df刮 数据。
我有 2 个与金钱相关的列,但它们是 chr 向量,因为它们是从网络上抓取的,如前所述。我可以申请一栏 trimws
没有问题,但不是另一个。
str(lacuracao_tvs$precio_actual[1])
chr " 1199.00"
为什么? new_precio_actual <- trimws(lacuracao_tvs$precio_actual[1])
dput(new_precio_actual)
" 1199.00"
trimws 在 precio_antes 中有效,但在 precio_actual 中无效:> str(lacuracao_tvs)
'data.frame': 100 obs. of 4 variables:
$ ecommerce : chr "la-curacao" "la-curacao" "la-curacao" "la-curacao" ...
$ producto : chr "TV LED AOC Ultra HD Smart 50\" LE50U7970" "TV Samsung Ultra HD 4K Smart 58\" UN-58RU7100G" "TV LG Ultra HD 4K Smart AI 55\" 55UK6200" "TV AOC Ultra HD 4K Smart 55\" 55U6285" ...
$ precio_antes : chr "1899.00" "1899.00" "1899.00" "1899.00" ...
$ precio_actual: chr " 1199.00" " 1199.00" " 1199.00" " 1199.00" ...
session 信息:Sys.info()
sysname release version nodename
"Windows" "10 x64" "build 17763" "DESKTOP-MNDUKBD"
machine login user effective_user
"x86-64" "OGONZALES" "OGONZALES" "OGONZALES"
> sessionInfo(package = NULL)
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
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 base
other attached packages:
[1] dplyr_0.7.8 rvest_0.3.2 xml2_1.2.0 RSelenium_1.7.5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 rstudioapi_0.9.0 bindr_0.1.1 magrittr_1.5
[5] rappdirs_0.3.1 tidyselect_0.2.5 R6_2.3.0 rlang_0.3.1
[9] stringr_1.3.1 httr_1.4.0 caTools_1.17.1.1 tools_3.5.2
[13] binman_0.1.1 selectr_0.4-1 semver_0.2.0 subprocess_0.8.3
[17] yaml_2.2.0 openssl_1.1 assertthat_0.2.0 tibble_2.0.1
[21] crayon_1.3.4 bindrcpp_0.2.2 purrr_0.2.5 bitops_1.0-6
[25] curl_3.3 glue_1.3.0 wdman_0.2.4 stringi_1.2.4
[29] compiler_3.5.2 pillar_1.3.1 XML_3.98-1.20 jsonlite_1.6
[33] pkgconfig_2.0.2
更新 1:utf8ToInt(lacuracao_tvs$precio_actual[1])
[1] 160 49 49 57 57 46 48 48
最佳答案
ASCII 码为 160 的字符称为“不间断空格”。人们可以在维基百科中阅读它:
https://en.wikipedia.org/wiki/Non-breaking_spacetrimws()
函数不将其包含在被函数删除的字符列表中:
x <- intToUtf8(c(160,49,49,57,57,46,48,48))
x
#[1] " 1199.00"
trimws(x)
#[1] " 1199.00"
摆脱它的一种方法是使用 str_trim()
stringr 库中的函数:library(stringr)
y <- str_trim(x)
trimws(y)
[1] "1199.00"
另一种方法是通过申请 iconv()
功能第一:y <- iconv(x, from = 'UTF-8', to = 'ASCII//TRANSLIT')
trimws(y)
#[1] "1199.00"
更新 解释为什么 trimws() 不会删除上述“不可见”字符而 stringr::str_trim() 会删除。
这是我们从
trimws()
中读到的内容帮助:For portability, ‘whitespace’ is taken as the character class [ \t\r\n] (space, horizontal tab, line feed, carriage return)
对于
stringr::str_trim()
帮助主题本身并没有指定什么被认为是“空白”,但是如果您查看 stri_trim_both
的帮助信息由 str_trim()
调用你会看到:stri_trim_both(str, pattern = "\\P{Wspace}")
基本上在这种情况下,它使用更广泛的字符,这些字符被视为空白。更新 2
正如@H1 所指出的,3.6.0 版提供了一个选项来指定考虑空白字符的内容:
Internally, 'sub(re, "", *, perl = TRUE)', i.e., PCRE library regular expressions are used. For portability, the default 'whitespace' is the character class '[ \t\r\n]' (space, horizontal tab, carriage return, newline). Alternatively, '[\h\v]' is a good (PCRE) generalization to match all Unicode horizontal and vertical white space characters, see also <URL: https://www.pcre.org>.
因此,如果您使用的是 3.6.0 或更高版本,您可以简单地执行以下操作:
> trimws(x,whitespace = "[\\h\\v]")
#[1] "1199.00"
关于r - 当trimws不起作用时如何修剪空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56782045/