用R语言处理字符串,相比于perl和python来说,比较麻烦。不能用下标提取,也不能用循环遍历索引。而R自身的字符串处理函数,如sub()、grep()等函数又增加的记忆负担。随着使用R语言的场景越来越多,字符串处理是必不可少的。给大家推荐一个由 Hadley Wickham 开发的一个灵活的字符串处理包stringr。
stringr的安装和其他安装包一样,进入到R的命令界面,运行以下命令并加载:
install.packages('stringr')
library(stringr)
stringr处理字符串是以正则表达式为基础的,所以在学习和使用stringr的时候最好能对正则表达式有一定的了解。
1)字符串的长度
# 字符串长度,类似R基础函数的nchar
str_length(c('a', 'R for datascience', NA))
#> [1] 1 18 NA
#
# fruit <- c('apple','banana',="" 'pear',="">->
str_count(fruit, 'a') # 返回向量fruit中可以匹配字母a的个数
#> [1] 1 3 1 1
2)字符串拼接函数:
str_c() 把多个字符串拼接起来,类似str_join和R基础函数paste
str_c('x', 'y')
#> [1] 'xy'
str_c('x', 'y', 'z')
#> [1] 'xyz'
使用sep参数,设定分隔符:
str_c('x', 'y', sep =',')
#> [1] 'x, y'
和其他R函数一样,缺失值会忽略. 如果你想输出确实值为 'NA',需要用到str_replace_na()函数:
x <>c('abc', NA)
str_c('|-', x, '-|')
#> [1] '|-abc-|' NA
str_c('|-', str_replace_na(x), '-|')
#> [1] '|-abc-|''|-NA-|'
上面的事例可以看出,str_c()是向量化处理的,自动循环向量,输出同样长度的向量:
str_c('prefix-', c('a', 'b', 'c'), '-suffix')
#> [1] 'prefix-a-suffix''prefix-b-suffix' 'prefix-c-suffix'
如果结合if语句,会选择性的输出想要的结果,如下:
name <>'Hadley'
time_of_day <>'morning'
birthday <>FALSE
str_c(
'Good ',time_of_day, ' ', name,
if(birthday) ' and HAPPY BIRTHDAY',
'.'
)
#> [1] 'Good morning Hadley.'
如果需要把向量也链接起来,需要使用参数 collapse:
str_c(c('x', 'y', 'z'), collapse =', ')
#> [1] 'x, y, z'
3)字符串提取:
字符串提取函数是str_sub,有两个参数start和end
str_sub(string, start, end)
x <>
c(
'Apple',
'Banana',
'Pear')
str_sub(x,
1,
3)
#> [1] 'App' 'Ban' 'Pea'
# negative numbers count backwards from end
str_sub(x, -
3, -
1)
#> [1] 'ple' 'ana' 'ear'
需要注意的是,如果字符串太短,是str_sub并不会报错,会尽可能的返回匹配的结果:
str_sub(
'a',
1,
5)
#> [1] 'a'
你也可以是 str_sub()
去进行替换和修改匹配的字符串:
x <>
c(
'Apple',
'Banana',
'Pear')
str_sub(x,
1,
1) <>
str_to_lower(
str_sub(x,
1,
1))
x
#> [1] 'apple' 'banana' 'pear'
4)大小写转换:
# 参数locale用于设定转换的语言,默认是英语
# 转换成大写
str_to_upper(
c(
'i',
'h'))
#> [1] 'I' 'H'
# 转换成小写
str_to_lower(
c(
'I',
'H'),
locale = “en)
#> [1] 'i' 'h'
# 首字母转换成大写
x <>
c(
'apple',
'banana',
'pear')
str_to_title()
#> [1] 'Apple',
'Banana',
'Pear'
5)字符串排序:
x <>
c(
'apple',
'eggplant',
'banana')
str_sort(x,
locale ='en')
# English
#> [1] 'apple' 'banana' 'eggplant'
str_order(x)
# 返回索引
#> [1] 1 3 2
x[str_order(x)
] #结果和str_sort一样
#> [1] 'apple' 'banana' 'eggplant'
6)检查字符串是否匹配:
检查字符串是否匹配成功,并返回逻辑值
x <>c('apple', 'banana', 'pear')
str_detect(x, 'e')
#> [1] TRUE FALSE TRUE
# 结合sum函数可以统计有多少个匹配成功
sum(str_detect(x, 'e'))
#> [1] 2
# 使用!符合可以得到相反的结果
!str_detect(x, 'e')
#> [1] FALSE TRUE FALSE
# 可以使用正在匹配的模式
x[str_detect(x, 'e')]
#> [1] 'apple' 'pear'
x[str_detect(x, 'e$')] # 美元符号$,在正则表达式里面表示末尾匹配
#> [1] 'pear'
7)替换
str_replace(string, pattern,replacement)
str_replace_all(string, pattern,replacement)
以上两个函数用于替换匹配成功的字符,区别是str_replace指替换第一个匹配成功的字符,str_replace_all替换全部匹配成功的字符:
x <>c('apple', 'pear', 'banana')
str_replace(x, 'a', '-') #只替换a为-,而且只替换一次
#> [1] '-pple' 'pe-r' 'b-nana'
str_replace_all(x, 'a', '-') #替换全部a为-
#> [1] '-pple' 'pe-r' 'b-n-n-'
str_replace(x, '[aeiou]', '-') #[aeiou]表示替换中括号中其中之一
#> [1] '-pple' 'p-ar' 'b-nana'
str_replace_all(x, '[aeiou]', '-') #[aeiou]表示替换中括号中所有
#> [1] '-ppl-''p--r' 'b-n-n-'
# 其中str_repalce_all,可以写成以下字典的模式:
x <>c('1house', '2cars', '3people')
str_replace_all(x, c('1' = 'one', '2' = 'two', '3' = 'three'))
#> [1] 'one house' 'two cars' 'three people'
8)返回匹配的字符串
str_subset(string, pattern)
val <-c('abc', 123,="">-c('abc',>
# 全文匹配
str_subset(val,'a')
[1] 'abc' 'cba'
# 开头匹配
str_subset(val,'^a')
[1] 'abc'
# 结尾匹配
str_subset(val,'a$')
[1] 'cba'
9)字符串分割
x <>
'This is a sentence. This is another sentence.'
#以空格分割字符串,返回的是一个list
str_split(x,
' ')
,
[[1]]
[1] 'This' 'is' 'a' 'sentence.' '' 'This' 'is'
[8] 'another' 'sentence.'
#以空格分割字符串,返回的是一个list,可以用下标提取,使其成为一个向量
str_split(x,
' ')[[
1]]
#> [1] 'This' 'is' 'a' 'sentence.' '' 'This'
#> [7] 'is' 'another' 'sentence.'
# 使用boundary('word')这种方式,是以word的边界为分割,可以提出标点符号以及多余的空格,如下:
str_split(x,
boundary(
'word'))[[
1]]
#> [1] 'This' 'is' 'a' 'sentence' 'This' 'is'
#> [7] 'another' 'sentence'
其他方法不常用,
str_trim(): 去掉字符串的空格和TAB(\t)
str_pad(): 补充字符串的长度
str_dup(): 复制字符串
str_wrap(): 控制字符串输出格式
str_locate: 找到匹配的字符串的位置。
str_conv: 字符编码转换
/End.
联系客服