1. stringr介绍

stringr包被定义为一致的、简单易用的字符串工具集。所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理。

字符串处理虽然不是R语言中最主要的功能,却也是必不可少的,数据清洗、可视化等的操作都会用到。对于R语言本身的base包提供的字符串基础函数,随着时间的积累,已经变得很多地方不一致,不规范的命名,不标准的参数定义,很难看一眼就上手使用。字符串处理在其他语言中都是非常方便的事情,R语言在这方面确实落后了。stringr包就是为了解决这个问题,让字符串处理变得简单易用,提供友好的字符串操作接口。


2. stringr安装

本文所使用的系统环境

> install.packages('stringr')

> library(stringr)

3. stringr的API介绍
stringr包1.0.0版本,一共提供了30个函数,方便我们对字符串处理。常用的字符串的处理以str_开头来命名,方便更直观理解函数的定义。我们可以根据使用习惯对函数进行分类:

 

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

3.2 字符串计算函数
3.2.1 str_count: 字符串计数
3.2.2 str_length: 字符串长度
3.2.3 str_sort: 字符串值排序
    str_order: 字符串索引排序,规则同str_sort

3.3 字符串匹配函数
3.3.1 str_split: 字符串分割
    str_split_fixed: 字符串分割,同str_split
3.3.2 str_subset: 返回匹配的字符串
3.3.3 word: 从文本中提取单词
3.3.4 str_detect: 检查匹配字符串的字符
3.3.6 str_match: 从字符串中提取匹配组。
    str_match_all: 从字符串中提取匹配组,同str_match
3.3.7 str_replace: 字符串替换
    str_replace_all: 字符串替换,同str_replace
3.3.8 str_replace_na:把NA替换为NA字符串
3.3.9 str_locate: 找到匹配的字符串的位置。
    str_locate_all: 找到匹配的字符串的位置,同str_locate
3.3.10 str_extract: 从字符串中提取匹配字符
      str_extract_all: 从字符串中提取匹配字符,同str_extract

3.4 字符串变换函数
3.4.1 str_conv: 字符编码转换
3.4.2 str_to_upper: 字符串转成大写
    str_to_lower: 字符串转成小写,规则同str_to_upper
    str_to_title: 字符串转成首字母大写,规则同str_to_upper

参数控制函数,仅用于构造功能的参数,不能独立使用。
boundary: 定义使用边界
coll: 定义字符串标准排序规则。
fixed: 定义用于匹配的字符,包括正则表达式中的转义符
regex: 定义正则表达式

 

3.1 字符串拼接函数

3.1.1 str_c,字符串拼接操作,与str_join完全相同,与paste()行为不完全一致。
str_c(..., sep = "", collapse = NULL)
str_join(..., sep = "", collapse = NULL) sep: 把多个字符串拼接为一个大的字符串,用于字符串的分割符。
 collapse: 把多个向量参数拼接为一个大的字符串,用于字符串的分割符。把多个字符串拼接为一个大的字符串。3.1.2 str_trim:去掉字符串的空格和TAB(\t)
str_trim(string, side = c("both", "left", "right"))
 string: 字符串,字符串向量。
 side: 过滤方式,both两边都过滤,left左边过滤,right右边过滤
 去掉字符串的空格和TAB(\t)3.1.3 str_pad:补充字符串的长度
str_pad(string, width, side = c("left", "right", "both"), pad = " ")
 string: 字符串,字符串向量。
 width: 字符串填充后的长度
 side: 填充方向,both两边都填充,left左边填充,right右边填充
 pad: 用于填充的字符
 补充字符串的长度。3.1.4 str_dup: 复制字符串
str_dup(string, times)
 string: 字符串,字符串向量。
 times: 复制数量
 复制一个字符串向量。3.1.5 str_wrap,控制字符串输出格式
str_wrap(string, width = 80, indent = 0, exdent = 0)
 string: 字符串,字符串向量。
 width: 设置一行所占的宽度。
 indent: 段落首行的缩进值
 exdent: 段落非首行的缩进值3.1.6 str_sub,截取字符串
str_sub(string, start = 1L, end = -1L)
 string: 字符串,字符串向量。
 start : 开始位置
 end : 结束位置
 截取字符串。

3.2 字符串计算函数

3.2.1 str_count, 字符串计数

str_count(string, pattern = "")

string: 字符串,字符串向量。
pattern: 匹配的字符。
对字符串中匹配的字符计数

3.2.2 str_length,字符串长度

str_length(string)

string: 字符串,字符串向量。

3.2.3 str_sort, 字符串值排序,同str_order索引排序

str_sort(x, decreasing = FALSE, na_last = TRUE, locale = "", ...)
str_order(x, decreasing = FALSE, na_last = TRUE, locale = "", ...)

x: 字符串,字符串向量。
decreasing: 排序方向。
na_last:NA值的存放位置,一共3个值,TRUE放到最后,FALSE放到最前,NA过滤处理
locale:按哪种语言习惯排序
对字符串值进行排序。

# 按ASCII字母排序
> str_sort(c('a',1,2,'11'), locale = "en")
[1] "1" "11" "2" "a"
# 倒序排序
> str_sort(letters,decreasing=TRUE)
[1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j" "i" "h"
[20] "g" "f" "e" "d" "c" "b" "a"
# 按拼音排序
> str_sort(c('你','好','粉','丝','日','志'),locale = "zh")
[1] "粉" "好" "你" "日" "丝" "志"
对NA值的排序处理
#把NA放最后面
> str_sort(c(NA,'1',NA),na_last=TRUE)
[1] "1" NA NA
#把NA放最前面
> str_sort(c(NA,'1',NA),na_last=FALSE)
[1] NA NA "1"
#去掉NA值
> str_sort(c(NA,'1',NA),na_last=NA)
[1] "1"

3.3 字符串匹配函数

3.3.1 str_split,字符串分割,同str_split_fixed
str_split(string, pattern, n = Inf)
str_split_fixed(string, pattern, n) string: 字符串,字符串向量。
 pattern: 匹配的字符。
 n: 分割个数
 对字符串进行分割。3.3.2 str_subset:返回的匹配字符串
str_subset(string, pattern)
 string: 字符串,字符串向量。
 pattern: 匹配的字符。3.3.3 word, 从文本中提取单词
word(string, start = 1L, end = start, sep = fixed(" "))
 string: 字符串,字符串向量。
 start: 开始位置。
 end: 结束位置。
 sep: 匹配字符。3.3.4 str_detect匹配字符串的字符
str_detect(string, pattern)
 string: 字符串,字符串向量。
 pattern: 匹配字符。3.3.6 str_match,从字符串中提取匹配组
str_match(string, pattern)
str_match_all(string, pattern) string: 字符串,字符串向量。
 pattern: 匹配字符。#从字符串中提取匹配组
 > val <- c("abc", 123, "cba")
# 匹配字符a,并返回对应的字符
 > str_match(val, "a")
 [,1]
 [1,] "a" 
 [2,] NA 
 [3,] "a" 
# 匹配字符0-9,限1个,并返回对应的字符
 > str_match(val, "[0-9]")
 [,1]
 [1,] NA 
 [2,] "1" 
 [3,] NA 
# 匹配字符0-9,不限数量,并返回对应的字符
 > str_match(val, "[0-9]*")
 [,1] 
 [1,] "" 
 [2,] "123"
 [3,] "" 
#从字符串中提取匹配组,以字符串matrix格式返回
 > str_match_all(val, "a")
 [[1]]
 [,1]
 [1,] "a" [[2]]
 [,1] [[3]]
 [,1]
 [1,] "a" > str_match_all(val, "[0-9]")
 [[1]]
 [,1] [[2]]
 [,1]
 [1,] "1" 
 [2,] "2" 
 [3,] "3" [[3]]
 [,1]3.3.7 str_replace,字符串替换
str_replace(string, pattern, replacement)
 string: 字符串,字符串向量。
 pattern: 匹配字符。
 replacement: 用于替换的字符。3.3.8 str_replace_na把NA替换为NA字符串
str_replace_na(string, replacement = "NA")
 string: 字符串,字符串向量。
 replacement : 用于替换的字符。
 把NA替换为字符串3.3.9 str_locate,找到的模式在字符串中的位置。
str_locate(string, pattern)
str_locate_all(string, pattern) string: 字符串,字符串向量。
 pattern: 匹配字符。3.3.10 str_extract从字符串中提取匹配模式
str_extract(string, pattern)
str_extract_all(string, pattern, simplify = FALSE) string: 字符串,字符串向量。
 pattern: 匹配字符。
 simplify: 返回值,TRUE返回matrix,FALSE返回字符串向量

3.4 字符串变换函数

3.4.1 str_conv:字符编码转换
str_conv(string, encoding)
 string: 字符串,字符串向量。
 encoding: 编码名。

#对中文进行转码处理。
 # 把中文字符字节化
 > x <- charToRaw('你好');x
 [1] c4 e3 ba c3
# 默认win系统字符集为GBK,GB2312为GBK字集,转码正常
 > str_conv(x, "GBK")
 [1] "你好"
 > str_conv(x, "GB2312")
 [1] "你好" # 转UTF-8失败
 > str_conv(x, "UTF-8")
 [1] "���"
 Warning messages:
 1: In stri_conv(string, encoding, "UTF-8") :
 input data \xffffffc4 in current source encoding could not be converted to Unicode
 2: In stri_conv(string, encoding, "UTF-8") :
 input data \xffffffe3\xffffffba in current source encoding could not be converted to Unicode
 3: In stri_conv(string, encoding, "UTF-8") :
 input data \xffffffc3 in current source encoding could not be converted to Unicode
#把unicode转UTF-8
 > x1 <- "\u5317\u4eac"
 > str_conv(x1, "UTF-8")
 [1] "北京"3.4.2 str_to_upper,字符串大写转换。
str_to_upper(string, locale = "")
str_to_lower(string, locale = "")
str_to_title(string, locale = "")
 string: 字符串。
 locale:按哪种语言习惯排序