stringr包基础操作

作者: 柳叶刀与小鼠标 | 来源:发表于2020-10-18 15:01 被阅读0次

stringr构建在stringi之上,stringr专注于最重要且最常用的字符串操作函数,而stringi提供了涵盖几乎所有可以想象的内容的全面集合。 如果发现stringr缺少所需的功能,请尝试查看stringi。

安装

# Install the released version from CRAN:
install.packages("stringr")

# Install the cutting edge development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/stringr")
  • 基础操作
x <- c("why", "video", "cross", "extra", "deal", "authority")
str_length(x)
#> [1] 3 5 5 5 4 9
str_c(x, collapse = ", ")
#> [1] "why, video, cross, extra, deal, authority"
str_sub(x, 1, 2)
#> [1] "wh" "vi" "cr" "ex" "de" "au"

大多数字符串函数都使用正则表达式,这是一种用于描述文本模式的简洁语言。 例如,正则表达式“ [aeiou]”匹配作为元音的任何单个字符:

str_subset(x, "[aeiou]")
#> [1] "video"     "cross"     "extra"     "deal"      "authority"
str_count(x, "[aeiou]")
#> [1] 0 3 1 2 2 4
  • str_detect(x, pattern) tells you if there’s any match to the pattern.
str_detect(x, "[aeiou]")
#> [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
  • str_count(x, pattern) 计数
str_count(x, "[aeiou]")
#> [1] 0 3 1 2 2 4
  • str_subset(x, pattern) 提取匹配的字符串
str_subset(x, "[aeiou]")
#> [1] "video"     "cross"     "extra"     "deal"      "authority"
  • str_locate(x, pattern) 定位匹配的字符串的位置
str_locate(x, "[aeiou]")
#>      start end
#> [1,]    NA  NA
#> [2,]     2   2
#> [3,]     3   3
#> [4,]     1   1
#> [5,]     2   2
#> [6,]     1   1
  • str_extract(x, pattern) 提取字符串
- str_extract(x, "[aeiou]")
#> [1] NA  "i" "o" "e" "e" "a"

  • str_match(x, pattern) 匹配字符串
# extract the characters on either side of the vowel
str_match(x, "(.)[aeiou](.)")
#>      [,1]  [,2] [,3]
#> [1,] NA    NA   NA  
#> [2,] "vid" "v"  "d" 
#> [3,] "ros" "r"  "s" 
#> [4,] NA    NA   NA  
#> [5,] "dea" "d"  "a" 
#> [6,] "aut" "a"  "t"
  • str_replace(x, pattern, replacement) 替换字符串
str_replace(x, "[aeiou]", "?")
#> [1] "why"       "v?deo"     "cr?ss"     "?xtra"     "d?al"      "?uthority"
  • str_split(x, pattern) 将字符串分割
str_split(c("a,b", "c,d,e"), ",")
#> [[1]]
#> [1] "a" "b"
#> 
#> [[2]]
#> [1] "c" "d" "e"

相关文章

网友评论

    本文标题:stringr包基础操作

    本文链接:https://www.haomeiwen.com/subject/etrcmktx.html