stringr包主要功能处理字符串,对字符串进行分割、连接、转换等操作。
install.packages('stringr')
library(stringr)
1、字符串拼接函数
str_c(..., sep = "", collapse = NULL),与str_join完全相同,与paste()行为不完全一致
参数:
sep: 把多个字符串拼接为一个大的字符串,用于字符串的分割符
collapse: 把多个向量参数拼接为一个大的字符串,用于字符串的分割符
#把多个字符串拼接为一个大的字符串
> str_c('a','b')
[1] "ab"
> str_c('a','b',sep='-')
[1] "a-b"
> str_c(c('a','a1'),c('b','b1'),sep='-')
[1] "a-b" "a1-b1"
#把多个向量参数拼接为一个大的字符串
> str_c(head(letters), collapse = "")
[1] "abcdef"
> str_c(head(letters), collapse = ", ")
[1] "a, b, c, d, e, f"
#collapse参数,对多个字符串无效
> str_c('a','b',collapse = "-")
[1] "ab"
> str_c(c('a','a1'),c('b','b1'),collapse='-')
[1] "ab-a1b1"
#拼接有NA值的字符串向量时,NA还是NA
> str_c(c("a", NA, "b"), "-d")
[1] "a-d" NA "b-d"
#对比str_c()函数和paste()函数之间的不同点
> str_c('a','b')
[1] "ab"
> paste('a','b') #多字符串拼接,默认的sep参数行为不一致
[1] "a b"
str_c(c("a", NA, "b"), "-d")
[1] "a-d" NA "b-d"
> paste(c("a", NA, "b"), "-d") #拼接有NA值的字符串向量,对NA的处理行为不一致
[1] "a -d" "NA -d" "b -d"
2、去掉字符串的空格和TAB(\t)
str_trim(string, side = c("both", "left", "right"))
string: 字符串,字符串向量
side: 过滤方式,both两边都过滤,left左边过滤,right右边过滤
#只过滤左边的空格
> str_trim(" left space\t\n",side='left')
[1] "left space\t\n"
#只过滤右边的空格
> str_trim(" left space\t\n",side='right')
[1] " left space"
#过滤两边的空格
> str_trim(" left space\t\n",side='both')
[1] "left space"
> str_trim("\nno space\n\t")
[1] "no space"
3、补充字符串的长度
str_pad(string, width, side = c("left", "right", "both"), pad = " ")
string: 字符串,字符串向量
width: 字符串填充后的长度
side: 填充方向,both两边都填充,left左边填充,right右边填充
pad: 用于填充的字符
#从左边补充空格,直到字符串长度为20
> str_pad("conan", 20, "left")
[1] " conan"
#从右边补充空格,直到字符串长度为20
> str_pad("conan", 20, "right")
[1] "conan "
#从左右两边各补充空格,直到字符串长度为20
> str_pad("conan", 20, "both")
[1] " conan "
#从左右两边各补充x字符,直到字符串长度为20
> str_pad("conan", 20, "both",'x')
[1] "xxxxxxxconanxxxxxxxx"
4、复制字符串
str_dup(string, times)
参数:
string: 字符串,字符串向量
times: 复制数量
复制一个字符串向量
> val <- c("abca4", 123, "cba2")
> str_dup(val, 2) #复制2次
[1] "abca4abca4" "123123" "cba2cba2"
> str_dup(val, 1:3) #按位置复制
[1] "abca4" "123123" "cba2cba2cba2"
5、截取字符串
str_sub(string, start = 1L, end = -1L)
参数:
string: 字符串,字符串向量
start : 开始位置
end : 结束位置
> txt <- "I am Conan."
#截取1-4的索引位置的字符串
> str_sub(txt, 1, 4)
[1] "I am"
#截取1-6的索引位置的字符串
> str_sub(txt, end=6)
[1] "I am C"
#截取6到结束的索引位置的字符串
> str_sub(txt, 6)
[1] "Conan."
#分2段截取字符串
> str_sub(txt, c(1, 4), c(6, 8))
[1] "I am C" "m Con"
#通过负坐标截取字符串
> str_sub(txt, -3)
[1] "an."
> str_sub(txt, end = -3)
[1] "I am Cona"
#对截取的字符串进行赋值
> x <- "AAABBBCCC"
> str_sub(x, 1, 1) <- 1; x #在字符串的1的位置赋值为1
[1] "1AABBBCCC"
> str_sub(x, 2, -2) <- "2345"; x #在字符串从2到-2的位置赋值为2345
[1] "12345C"
6、字符串计算函数
str_count(string, pattern = "")
参数:
string: 字符串,字符串向量
pattern: 匹配的字符
#对字符串中匹配的字符计数
> str_count('aaa444sssddd', "a")
[1] 3
#对字符串向量中匹配的字符计数
> fruit <- c("apple", "banana", "pear", "pineapple")
> str_count(fruit, "a")
[1] 1 3 1 1
> str_count(fruit, "p")
[1] 2 0 1 3
#对字符串中的'.'字符计数
> str_count(c("a.", ".", ".a.",NA), ".") #由于.是正则表达式的匹配符,直接判断计数的结果是不对的
[1] 2 1 3 NA
> str_count(c("a.", ".", ".a.",NA), fixed(".")) #用fixed匹配字符
[1] 1 1 2 NA
> str_count(c("a.", ".", ".a.",NA), "\\.") #用\\匹配字符
[1] 1 1 2 NA
7、字符串长度
str_length(string)
参数:
string: 字符串,字符串向量
#计算字符串的长度
> str_length(c("I", "am", "张丹", NA))
[1] 1 2 2 NA
8、字符串值排序,同str_order索引排序
str_sort(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" "g" "f" "e" "d" "c" "b" "a"
#按拼音排序
> str_sort(c('你','好','粉','丝','日','志'),locale = "zh")
[1] "粉" "好" "你" "日" "丝" "志"
#对NA值排序
> str_sort(c(NA,'1',NA),na_last=TRUE) #把NA放最后面
[1] "1" NA NA
> str_sort(c(NA,'1',NA),na_last=FALSE) #把NA放最前面
[1] NA NA "1"
> str_sort(c(NA,'1',NA),na_last=NA) #去掉NA值
[1] "1"
9、字符串分割,同str_split_fixed
str_split(string, pattern, n = Inf)
参数:
string: 字符串,字符串向量
pattern: 匹配的字符
n: 分割个数
> val <- "abc,123,234,iuuu"
#以,进行分割
> str_split(val, ",")
[[1]]
[1] "abc" "123" "234" "iuuu"
#以,进行分割,保留2块
> str_split(val, ",",2)
[[1]]
[1] "abc" "123,234,iuuu"
#用str_split_fixed()函数分割,结果类型是matrix
> str_split_fixed(val, ",",2)
[,1] [,2]
[1,] "abc" "123,234,iuuu"
10、返回的匹配字符串
str_subset(string, pattern)
参数:
string: 字符串,字符串向量
pattern: 匹配的字符
> val <- c("abc", 123, "cba")
#全文匹配
> str_subset(val, "a")
[1] "abc" "cba"
#开头匹配
> str_subset(val, "^a")
[1] "abc"
#结尾匹配
> str_subset(val, "a$")
[1] "cba"
11、从文本中提取单词
word(string, start = 1L, end = start, sep = fixed(" "))
参数:
string: 字符串,字符串向量
start: 开始位置
end: 结束位置
sep: 匹配字符
#默认以空格分割,取第一个位置的字符串
> val <- c("I am Conan.", "http://fens.me, ok")
> word(val, 1)
[1] "I" "http://fens.me,"
> word(val, -1)
[1] "Conan." "ok"
> word(val, 2, -1)
[1] "am Conan." "ok"
#以,分割,取第一个位置的字符串
> val<-'111,222,333,444'
> word(val, 1, sep = fixed(','))
[1] "111"
> word(val, 3, sep = fixed(','))
[1] "333"
12、匹配字符串的字符
str_detect(string, pattern)
参数:
string: 字符串,字符串向量
pattern: 匹配字符
> val <- c("abca4", 123, "cba2")
#检查字符串向量,是否包括a
> str_detect(val, "a")
[1] TRUE FALSE TRUE
#检查字符串向量,是否以a为开头
> str_detect(val, "^a")
[1] TRUE FALSE FALSE
#检查字符串向量,是否以a为结尾
> str_detect(val, "a$")
[1] FALSE FALSE FALSE
13、从字符串中提取匹配组
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]
14、字符串替换
str_replace(string, pattern, replacement)
参数:
string: 字符串,字符串向量
pattern: 匹配字符
replacement: 用于替换的字符
> val <- c("abc", 123, "cba")
#把目标字符串第一个出现的a或b,替换为-
> str_replace(val, "[ab]", "-")
[1] "-bc" "123" "c-a"
#把目标字符串所有出现的a或b,替换为-
> str_replace_all(val, "[ab]", "-")
[1] "--c" "123" "c--"
#把目标字符串所有出现的a,替换为被转义的字符
> str_replace_all(val, "[a]", "\1\1")
[1] "\001\001bc" "123" "cb\001\001"
15、字符串大小写转换
str_to_upper(string, locale = "")
str_to_lower(string, locale = "")
str_to_title(string, locale = "")
参数:
string: 字符串
locale:按哪种语言习惯排序
> val <- "I am conan. Welcome to my blog! http://fens.me"
> str_to_upper(val)
[1] "I AM CONAN. WELCOME TO MY BLOG! HTTP://FENS.ME"
> str_to_lower(val)
[1] "i am conan. welcome to my blog! http://fens.me"
> str_to_title(val)
[1] "I Am Conan. Welcome To My Blog! Http://Fens.me"
暂无数据