stringr是生信技能樹生信爆款入門課程R語言部分Day7的講到的一個(gè)重要知識(shí)點(diǎn)。為加深理解,現(xiàn)在找個(gè)數(shù)據(jù)做下練習(xí)鞏固。首先學(xué)習(xí)下這個(gè)函數(shù),然后再開始練習(xí)。
stringr常用函數(shù)介紹
stringr包常用的字符串的處理以str_開頭來命名,方便更直觀理解函數(shù)的定義。我們可以根據(jù)使用習(xí)慣對(duì)函數(shù)進(jìn)行分類:
字符串拼接函數(shù)
- str_c: 字符串拼接。
- str_join: 字符串拼接,同str_c。
- str_trim: 去掉字符串的空格和TAB(\t)
- str_pad: 補(bǔ)充字符串的長(zhǎng)度
- str_dup: 復(fù)制字符串
- str_wrap: 控制字符串輸出格式
- str_sub: 截取字符串
- str_sub<- 截取字符串,并賦值,同str_sub
字符串計(jì)算函數(shù)
- str_count: 字符串計(jì)數(shù)
- str_length: 字符串長(zhǎng)度
- str_sort: 字符串值排序
- str_order: 字符串索引排序,規(guī)則同str_sort
字符串匹配函數(shù)
- 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
字符串變換函數(shù)
- str_conv: 字符編碼轉(zhuǎn)換
- str_to_upper: 字符串轉(zhuǎn)成大寫
- str_to_lower: 字符串轉(zhuǎn)成小寫,規(guī)則同str_to_upper
- str_to_title: 字符串轉(zhuǎn)成首字母大寫,規(guī)則同str_to_upper
參數(shù)控制函數(shù),僅用于構(gòu)造功能的參數(shù),不能獨(dú)立使用。
- boundary: 定義使用邊界
- coll: 定義字符串標(biāo)準(zhǔn)排序規(guī)則。
- fixed: 定義用于匹配的字符,包括正則表達(dá)式中的轉(zhuǎn)義符
- regex: 定義正則表達(dá)式
stringr安裝及生成測(cè)試數(shù)據(jù)
> install.packages('stringr')
> library(stringr)
> x <- sentences[72]
> x
[1] "The two met while playing on the sand."
1.首先檢測(cè)變量x中有多少個(gè)元素
> length(x)#元素 是只有一個(gè)元素的句子
[1] 1
2.檢測(cè)x中有多少個(gè)字符
> str_count(x)
[1] 38
> str_length(x)
[1] 38
> nchar(x)#基礎(chǔ)函數(shù)
[1] 38
> #統(tǒng)計(jì)下x中空格個(gè)個(gè)數(shù)
> str_count(x," ")
[1] 7
> #統(tǒng)計(jì)下x中o的個(gè)數(shù)
> str_count(x2,"o")
[1] 0 1 0 0 0 1 0 0
3.檢測(cè)x中多少個(gè)字母
> #去除點(diǎn)和空格就是字母數(shù)??梢钥闯鼋y(tǒng)計(jì)字符是包含空格和標(biāo)點(diǎn)在內(nèi)的
> str_count(x)-str_count(x,' ')-str_count(x,'[.]')
[1] 30
> str_count(x)-str_count(x,' ')-str_count(x,'\\.')
[1] 30
> #這里要注意的是‘.’,需要用轉(zhuǎn)義符號(hào)\\或[]來實(shí)現(xiàn)它本來的意思。
> #具體可以搜索正則表達(dá)式。
4.字符串拆分
> #將x按照空格拆分成多個(gè)元素
> str_split(x," ")
[[1]]
[1] "The" "two" "met" "while" "playing" "on" "the" "sand."
> #可以看出這是一個(gè)只有一個(gè)元素的列表
> class(str_split(x," "))
[1] "list"
> #提取列表第一個(gè)元素
> x2 = str_split(x," ")[[1]]
> x2
[1] "The" "two" "met" "while" "playing" "on" "the" "sand."
5 字符串組合
#將x2 按照空格分割符組合成一個(gè)元素
> str_c(x2,collapse = " ")
[1] "The two met while playing on the sand."
> #將x2每個(gè)元素都加上.1的后綴
> str_c(x2,1,sep = ".")#元素內(nèi)聯(lián)
[1] "The.1" "two.1" "met.1" "while.1" "playing.1" "on.1"
[7] "the.1" "sand..1"
6.提取x的第5到8個(gè)元素
> str_sub(x,5,8)
[1] "two "
7.大小寫轉(zhuǎn)換
> #將x2全部轉(zhuǎn)換為大寫
> str_to_upper(x2)
[1] "THE" "TWO" "MET" "WHILE" "PLAYING" "ON" "THE" "SAND."
> #將x2全部轉(zhuǎn)換為小寫
> str_to_lower(x2)
[1] "the" "two" "met" "while" "playing" "on" "the" "sand."
> #將x2全部轉(zhuǎn)化為首字母大寫
> str_to_title(x2)#
[1] "The" "Two" "Met" "While" "Playing" "On" "The" "Sand."
>
8.將字符串x2按照首字母順序進(jìn)行排序
str_sort(x2)
[1] "met" "on" "playing" "sand." "the" "The" "two" "while"
>
9.字符檢測(cè)
#判斷x2中每個(gè)元素是否含有字母h
> str_detect(x2,"h")#重點(diǎn)中重點(diǎn) 得到邏輯值向量 取子集
[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結(jié)尾的元素
> str_ends(x2,"e")#開頭結(jié)尾
[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
> #提取x2中以T開頭的元素
> x2[str_starts(x2,"T")]
[1] "The"
> ###與sum和mean連用,可以統(tǒng)計(jì)匹配的個(gè)數(shù)和比例
> 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."