R语言学习7.1----stringr处理字符串

stringr是生信技能树生信爆款入门课程R语言部分Day7的讲到的一个重要知识点。为加深理解,现在找个数据做下练习巩固。首先学习下这个函数,然后再开始练习。

stringr常用函数介绍

stringr包常用的字符串的处理以str_开头来命名,方便更直观理解函数的定义。我们可以根据使用习惯对函数进行分类:

字符串拼接函数

  • str_c: 字符串拼接。
  • str_join: 字符串拼接,同str_c。
  • str_trim: 去掉字符串的空格和TAB(\t)
  • str_pad: 补充字符串的长度
  • str_dup: 复制字符串
  • str_wrap: 控制字符串输出格式
  • str_sub: 截取字符串
  • str_sub<- 截取字符串,并赋值,同str_sub

字符串计算函数

  • str_count: 字符串计数
  • str_length: 字符串长度
  • str_sort: 字符串值排序
  • str_order: 字符串索引排序,规则同str_sort

字符串匹配函数

  • str_split: 字符串分割
  • str_split_fixed: 字符串分割,同str_split
  • str_subset: 返回匹配的字符串
  • word: 从文本中提取单词
  • str_detect: 检查匹配字符串的字符
  • str_match: 从字符串中提取匹配组。
  • str_match_all: 从字符串中提取匹配组,同str_match
  • str_replace: 字符串替换
  • str_replace_all: 字符串替换,同str_replace
  • str_replace_na:把NA替换为NA字符串
  • str_locate: 找到匹配的字符串的位置。
  • str_locate_all: 找到匹配的字符串的位置,同str_locate
  • str_extract: 从字符串中提取匹配字符
  • str_extract_all: 从字符串中提取匹配字符,同str_extract

字符串变换函数

  • str_conv: 字符编码转换
  • str_to_upper: 字符串转成大写
  • str_to_lower: 字符串转成小写,规则同str_to_upper
  • str_to_title: 字符串转成首字母大写,规则同str_to_upper

参数控制函数,仅用于构造功能的参数,不能独立使用。

  • boundary: 定义使用边界
  • coll: 定义字符串标准排序规则。
  • fixed: 定义用于匹配的字符,包括正则表达式中的转义符
  • regex: 定义正则表达式

stringr安装及生成测试数据

> install.packages('stringr')
> library(stringr)
> x <- sentences[72]
> x
[1] "The two met while playing on the sand."

1.首先检测变量x中有多少个元素

> length(x)#元素 是只有一个元素的句子
[1] 1

2.检测x中有多少个字符

> str_count(x)
[1] 38
> str_length(x)
[1] 38
> nchar(x)#基础函数
[1] 38
> #统计下x中空格个个数
> str_count(x," ")
[1] 7
> #统计下x中o的个数
> str_count(x2,"o")
[1] 0 1 0 0 0 1 0 0

3.检测x中多少个字母

> #去除点和空格就是字母数。可以看出统计字符是包含空格和标点在内的
> str_count(x)-str_count(x,' ')-str_count(x,'[.]')
[1] 30
> str_count(x)-str_count(x,' ')-str_count(x,'\\.')
[1] 30
> #这里要注意的是‘.’,需要用转义符号\\或[]来实现它本来的意思。
> #具体可以搜索正则表达式。

4.字符串拆分

> #将x按照空格拆分成多个元素
> str_split(x," ")
[[1]]
[1] "The"     "two"     "met"     "while"   "playing" "on"      "the"     "sand."  

> #可以看出这是一个只有一个元素的列表
> class(str_split(x," "))
[1] "list"
> #提取列表第一个元素
> x2 = str_split(x," ")[[1]]
> x2
[1] "The"     "two"     "met"     "while"   "playing" "on"      "the"     "sand."  

5 字符串组合

 #将x2 按照空格分割符组合成一个元素
> str_c(x2,collapse = " ")
[1] "The two met while playing on the sand."
> #将x2每个元素都加上.1的后缀
> str_c(x2,1,sep = ".")#元素内联
[1] "The.1"     "two.1"     "met.1"     "while.1"   "playing.1" "on.1"     
[7] "the.1"     "sand..1"  

6.提取x的第5到8个元素

> str_sub(x,5,8)
[1] "two "

7.大小写转换

> #将x2全部转换为大写
> str_to_upper(x2)
[1] "THE"     "TWO"     "MET"     "WHILE"   "PLAYING" "ON"      "THE"     "SAND."  
> #将x2全部转换为小写
> str_to_lower(x2)
[1] "the"     "two"     "met"     "while"   "playing" "on"      "the"     "sand."  
> #将x2全部转化为首字母大写
> str_to_title(x2)#
[1] "The"     "Two"     "Met"     "While"   "Playing" "On"      "The"     "Sand."  
> 

8.将字符串x2按照首字母顺序进行排序

 str_sort(x2)
[1] "met"     "on"      "playing" "sand."   "the"     "The"     "two"     "while"  
> 

9.字符检测

 #判断x2中每个元素是否含有字母h
> str_detect(x2,"h")#重点中重点 得到逻辑值向量  取子集
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
> #判断x2中有没有以T开头的元素
> str_starts(x2,"T")
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> #判断x2中有没有以e结尾的元素
> str_ends(x2,"e")#开头结尾
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
> #提取x2中以T开头的元素
> x2[str_starts(x2,"T")]
[1] "The"
> ###与sum和mean连用,可以统计匹配的个数和比例
> sum(str_detect(x2,"h"))
[1] 3
> mean(str_detect(x2,"h"))
[1] 0.375
> as.numeric(str_detect(x2,"h"))
[1] 1 0 0 1 0 0 1 0

10.提取匹配到的字符串或字符串替换

 #提取x2中含有h的元素
> str_subset(x2,"h")
[1] "The"   "while" "the"  
> #将x2中所有o替换为2021
> str_replace(x2,"o","2021")#
[1] "The"     "tw2021"  "met"     "while"   "playing" "2021n"   "the"     "sand."  

参考
R语言字符串处理包
R----stringr包介绍学习

你可能感兴趣的:(R语言学习7.1----stringr处理字符串)