基本用法

查看长度

  1. x <- "The birch canoe slid on the smooth planks."
  2. length(x)
  3. str_length(x)
  4. > length(x)
  5. [1] 1
  6. > str_length(x)
  7. [1] 42

length 只会返回出x 中的元素数(长度为1 的字符串类型的向量)。

str_length 才会返回字符串长度。

拆分与组合

拆分

需要注意的是,提取拆分后的元素需要使用 [[]] 双括号选择。

  1. x <- "The birch canoe slid on the smooth planks."
  2. str_split(x," ")
  3. x2 = str_split(x," ")[[1]]
  4. # 此时x2 是一个包含x中所有单词(用空格拆分了)的向量。
  5. > length(x2)
  6. [1] 8

合并

collapse 设定合并向量中内容使用的分隔符。

  1. str_c(x2,collapse = " ")

collapse 参数设定分离的元素结合成一个字符串分离的符号。

还可以将两个向量中的元素,或向量和另外一个字符串进行合并。

  1. str_c(x2,1234,sep = "+")

sep 参数设定某两个分隔的元素连接,使用某符号。、

如果是长度不相等的两个向量合并,则会循环连接(挨个对上,而非全部对上):

  1. > c
  2. [1] 9 10 1 8 4 5 6 2 12 11 7 3
  3. > b
  4. [1] "a" "ppple"
  5. > str_c(c,b, sep='+')
  6. [1] "9+a" "10+ppple" "1+a" "8+ppple" "4+a" "5+ppple"
  7. [7] "6+a" "2+ppple" "12+a" "11+ppple" "7+a" "3+ppple"

提取字符串

str_sub(string, start = 1L, end = -1L) ,从start 到end, 为提取的string 中字符在字符串中的位置。(空格也占位!)

  1. x <- "The birch canoe slid on the smooth planks."
  2. str_sub(x,5,9)

大小写转换

upper 大写,lower 大写,title 首字母大写。

  1. str_to_upper(x2)
  2. str_to_lower(x2)
  3. str_to_title(x2)

字符串排序

默认按照英文字母或数字大小顺序。

  1. str_sort(x2)

空白处理

stringr::str_trim(string, side) 返回删去字符型向量 string 每个元素的首尾空格的结果,可以用 side 指定删除首尾空格(“both”)、开头空格(“left”)、末尾空格(“right”)。如:

006.02 stringr 处理字符串数据 - 图1

stringr::str_squish(string) 对字符型向量 string 每个元素,将重复空格变成单个,返回变换后的结果。

高级用法

字符检测

对字符串分隔后的向量与待检测的字符进行比较,生成等长的逻辑值向量。

detect 检测全字符,starts 检测首字母,ends 检测末字母。

  1. str_detect(x2,"h")
  2. str_starts(x2,"T")
  3. str_ends(x2,"e")
  4. > str_detect(x2,"h")
  5. [1] TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE

还可以联合 table 统计TRUE 与FALSE 的数目。

  1. > table(str_detect(x2,"h"))
  2. FALSE TRUE
  3. 4 4

另外,也可以联合 sum 获取TRUE 数目, mean 获得TRUE 比例。

  1. > sum(str_detect(x2,"h"))
  2. [1] 4
  3. > mean(str_detect(x2,"h"))
  4. [1] 0.5

提取匹配字符

将向量中符合要求的元素提取为一个新的向量。

  1. > x <- str_subset(x2,"h")
  2. > x
  3. [1] "The" "birch" "the" "smooth"

ps:匹配和检测支持正则:

006.02 stringr 处理字符串数据 - 图2

字符计数

计算字符串内指定字符出现次数。

  1. str_count(x," ")
  2. str_count(x2,"o")
  3. > str_count(x," ")
  4. [1] 7
  5. > str_count(x2,"o")
  6. [1] 0 0 1 0 1 0 2 0

字符替换与删除

replace 会替换字符串中指定的第一个字符。

而replace_all 则替换全部符合的字符。

  1. str_replace(x2,"o","A")
  2. str_replace_all(x2,"o","A")
  3. > str_replace(x2,"o","A")
  4. [1] "The" "birch" "canAe" "slid" "An" "the"
  5. [7] "smAoth" "planks."
  6. > str_replace_all(x2,"o","A")
  7. [1] "The" "birch" "canAe" "slid" "An" "the"
  8. [7] "smAAth" "planks."
  9. > > str_remove(x2, "[.]")
  10. [1] "The" "birch" "canoe" "slid" "on" "the"
  11. [7] "smooth" "planks"

str_remove 可以将指定的某个字符串从字符串中删除。

练习题

6-2

  1. #练习6-2
  2. library(stringr)
  3. #Bioinformatics is a new subject of genetic data collection,analysis and dissemination to the research community.
  4. tmp <- "Bioinformatics is a new subject of genetic data collection,analysis and dissemination to the research community."
  5. #1.将上面这句话作为一个长字符串,赋值给tmp
  6. #2.拆分为一个由单词组成的向量,赋值给tmp2(注意标点符号)
  7. tmp2 <- tmp %>%
  8. str_replace(',', ' ') %>%
  9. str_replace('\\.','') %>%
  10. str_split(" ")
  11. # 直接用或连字符, str_split(" |,") 可以省去替换, 操作
  12. tmp2 <- tmp2[[1]]
  13. tmp2
  14. #3.用函数返回这句话中有多少个单词。
  15. length(tmp2)
  16. #4.用函数返回这句话中每个单词由多少个字母组成。
  17. str_length(tmp2)
  18. #5.统计tmp2有多少个单词中含有字母"e"
  19. sum(str_detect(tmp2, "e"))