|
用R語言處理字符串,相比于perl和python來說,比較麻煩。不能用下標提取,也不能用循環(huán)遍歷索引。而R自身的字符串處理函數(shù),如sub()、grep()等函數(shù)又增加的記憶負擔。隨著使用R語言的場景越來越多,字符串處理是必不可少的。給大家推薦一個由 Hadley Wickham 開發(fā)的一個靈活的字符串處理包stringr。
stringr的安裝和其他安裝包一樣,進入到R的命令界面,運行以下命令并加載:
install.packages('stringr') library(stringr)
stringr處理字符串是以正則表達式為基礎的,所以在學習和使用stringr的時候最好能對正則表達式有一定的了解。
1)字符串的長度 # 字符串長度,類似R基礎函數(shù)的nchar str_length(c('a', 'R for datascience', NA)) #> [1] 1 18 NA # # fruit <- c('apple','banana',="" 'pear',="">-> str_count(fruit, 'a') # 返回向量fruit中可以匹配字母a的個數(shù) #> [1] 1 3 1 1
2)字符串拼接函數(shù): str_c() 把多個字符串拼接起來,類似str_join和R基礎函數(shù)paste
str_c('x', 'y') #> [1] 'xy' str_c('x', 'y', 'z') #> [1] 'xyz'
使用sep參數(shù),設定分隔符:
str_c('x', 'y', sep =',') #> [1] 'x, y'
和其他R函數(shù)一樣,缺失值會忽略. 如果你想輸出確實值為 'NA',需要用到str_replace_na()函數(shù):
x <>c('abc', NA) str_c('|-', x, '-|') #> [1] '|-abc-|' NA str_c('|-', str_replace_na(x), '-|') #> [1] '|-abc-|''|-NA-|' 上面的事例可以看出,str_c()是向量化處理的,自動循環(huán)向量,輸出同樣長度的向量:
str_c('prefix-', c('a', 'b', 'c'), '-suffix') #> [1] 'prefix-a-suffix''prefix-b-suffix' 'prefix-c-suffix'
如果結(jié)合if語句,會選擇性的輸出想要的結(jié)果,如下:
name <>'Hadley' time_of_day <>'morning' birthday <>FALSE
str_c( 'Good ',time_of_day, ' ', name, if(birthday) ' and HAPPY BIRTHDAY', '.' ) #> [1] 'Good morning Hadley.'
如果需要把向量也鏈接起來,需要使用參數(shù) collapse:
str_c(c('x', 'y', 'z'), collapse =', ') #> [1] 'x, y, z'
3)字符串提?。?/strong> 字符串提取函數(shù)是str_sub,有兩個參數(shù)start和end str_sub(string, start, end)
str_sub #> [1] 'App' 'Ban' 'Pea' # negative numbers count backwards from end str_sub #> [1] 'ple' 'ana' 'ear'
需要注意的是,如果字符串太短,是str_sub并不會報錯,會盡可能的返回匹配的結(jié)果:
str_sub #> [1] 'a' 你也可以是
str_sub
#> [1] 'apple' 'banana' 'pear'
4)大小寫轉(zhuǎn)換: # 參數(shù)locale用于設定轉(zhuǎn)換的語言,默認是英語
# 轉(zhuǎn)換成大寫 str_to_upper #> [1] 'I' 'H' # 轉(zhuǎn)換成小寫 str_to_lower #> [1] 'i' 'h' # 首字母轉(zhuǎn)換成大寫
str_to_title() #> [1] 'Apple'
5)字符串排序:
str_sort #> [1] 'apple' 'banana' 'eggplant' str_order #> [1] 1 3 2 x[str_order #> [1] 'apple' 'banana' 'eggplant'
6)檢查字符串是否匹配: 檢查字符串是否匹配成功,并返回邏輯值 x <>c('apple', 'banana', 'pear') str_detect(x, 'e') #> [1] TRUE FALSE TRUE # 結(jié)合sum函數(shù)可以統(tǒng)計有多少個匹配成功 sum(str_detect(x, 'e')) #> [1] 2 # 使用!符合可以得到相反的結(jié)果 !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)
以上兩個函數(shù)用于替換匹配成功的字符,區(qū)別是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'
# 結(jié)尾匹配 str_subset(val,'a$') [1] 'cba'
9)字符串分割
#以空格分割字符串,返回的是一個list str_split [[1]] [1] 'This' 'is' 'a' 'sentence.' '' 'This' 'is' [8] 'another' 'sentence.' #以空格分割字符串,返回的是一個list,可以用下標提取,使其成為一個向量 str_split #> [1] 'This' 'is' 'a' 'sentence.' '' 'This' #> [7] 'is' 'another' 'sentence.' # 使用boundary('word')這種方式,是以word的邊界為分割,可以提出標點符號以及多余的空格,如下: str_split #> [1] 'This' 'is' 'a' 'sentence' 'This' 'is' #> [7] 'another' 'sentence' 其他方法不常用, str_trim(): 去掉字符串的空格和TAB(\t) str_pad(): 補充字符串的長度 str_dup(): 復制字符串 str_wrap(): 控制字符串輸出格式 str_locate: 找到匹配的字符串的位置。 str_conv: 字符編碼轉(zhuǎn)換
/End. |
|
|