一般来说,我们需要一个矢量化的解决方案,所以这里有一个更好的测试示例:
whitespace <- " \t\n\r\v\f" # space, tab, newline,
# carriage return, vertical tab, form feed
x <- c(
" x y ", # spaces before, after and in between
" \u2190 \u2192 ", # contains unicode chars
paste0( # varied whitespace
whitespace,
"x",
whitespace,
"y",
whitespace,
collapse = ""
),
NA # missing
)
## [1] " x y "
## [2] " ← → "
## [3] " \t\n\r\v\fx \t\n\r\v\fy \t\n\r\v\f"
## [4] NA
gsub
gsub
用另一个字符串替换字符串(fixed = TRUE
)或正则表达式(fixed = FALSE
,默认值)的所有实例.要删除所有空格,请使用:
gsub(" ", "", x, fixed = TRUE)
## [1] "xy" "←→"
## [3] "\t\n\r\v\fx\t\n\r\v\fy\t\n\r\v\f" NA
正如DWin所指出的,在这种情况下,fixed = TRUE
不是必需的,但提供了更好的性能,因为匹配固定字符串比匹配正则表达式更快.
如果要删除所有类型的空白,请使用:
gsub("[[:space:]]", "", x) # note the double square brackets
## [1] "xy" "←→" "xy" NA
gsub("\\s", "", x) # same; note the double backslash
library(regex)
gsub(space(), "", x) # same
"[:space:]"
是与所有空格字符匹配的特定于R的正则表达式组.\s
是一个独立于语言的正则表达式,它也做同样的事情.
stringr
approach: str_replace_all
and str_trim
stringr
围绕基本R函数提供了更多人类可读的包装(尽管截至2014年12月,开发版本在stringi
之上构建了一个分支,如下所述).使用[str_replace_all][3]
]的上述命令的类似功能为:
library(stringr)
str_replace_all(x, fixed(" "), "")
str_replace_all(x, space(), "")
stringr
还有一个str_trim
函数,只删除前导和尾随空格.
str_trim(x)
## [1] "x y" "← →" "x \t\n\r\v\fy" NA
str_trim(x, "left")
## [1] "x y " "← → "
## [3] "x \t\n\r\v\fy \t\n\r\v\f" NA
str_trim(x, "right")
## [1] " x y" " ← →"
## [3] " \t\n\r\v\fx \t\n\r\v\fy" NA
stringi
approach: stri_replace_all_charclass
and stri_trim
stringi
构建在独立于平台的ICU library上,并具有一系列广泛的字符串操作功能.上述equivalents项是:
library(stringi)
stri_replace_all_fixed(x, " ", "")
stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")
这里"\\p{WHITE_SPACE}"
是一组被认为是空白的Unicode代码点的替代语法,相当于"[[:space:]]"
、"\\s"
和space()
.对于更复杂的正则表达式替换,还有stri_replace_all_regex
个.
stringi
也有trim functions个.
stri_trim(x)
stri_trim_both(x) # same
stri_trim(x, "left")
stri_trim_left(x) # same
stri_trim(x, "right")
stri_trim_right(x) # same