4.4 base 和 stringr

以下表格数据对比,主要是 base R 和 stringr 中相应字符处理功能函数对比。

表格数据来源stringr and base differences。表格数据可用以下代码获取(注意网络):

library(tidyverse)
library(rvest)
dt <- read_html('https://stringr.tidyverse.org/articles/from-base.html') %>% 
   html_table() %>% `[[`(1)
base stringr
gregexpr(pattern, x) str_locate_all(x, pattern)
grep(pattern, x, value = TRUE) str_subset(x, pattern)
grep(pattern, x) str_which(x, pattern)
grepl(pattern, x) str_detect(x, pattern)
gsub(pattern, replacement, x) str_replace_all(x, pattern, replacement)
nchar(x) str_length(x)
order(x) str_order(x)
regexec(pattern, x) + regmatches() str_match(x, pattern)
regexpr(pattern, x) + regmatches() str_extract(x, pattern)
regexpr(pattern, x) str_locate(x, pattern)
sort(x) str_sort(x)
strrep(x, n) str_dup(x, n)
strsplit(x, pattern) str_split(x, pattern)
strwrap(x) str_wrap(x)
sub(pattern, replacement, x) str_replace(x, pattern, replacement)
substr(x, start, end) str_sub(x, start, end)
tolower(x) str_to_lower(x)
tools::toTitleCase(x) str_to_title(x)
toupper(x) str_to_upper(x)
trimws(x) str_trim(x)

通过以上对比,方便我们从 Base R 切换到 stringr 包的使用。