《R語言實(shí)戰(zhàn)》學(xué)習(xí)筆記---Chapter5(4) 高級(jí)數(shù)據(jù)管理 字符處理函數(shù)

字符處理函數(shù)

tidyverse集合中有一個(gè)非常強(qiáng)大的處理字符串的包,可以看這篇文章R語言stringr包處理字符串 - 簡書 (jianshu.com)。這篇文章是閱讀《R語言實(shí)戰(zhàn)》的學(xué)習(xí)筆記,我們繼續(xù)書中的內(nèi)容。

注意,涉及到正則表達(dá)式的部分都先忽略,先把基礎(chǔ)的弄明白,正則表達(dá)式需要專門深入學(xué)習(xí)。

  • 計(jì)算字符數(shù)量
> x <- c("aghe", "1", "語言", "哈", "ahjdna")
> nchar(x)
[1] 4 1 2 1 6
> length(x)
[1] 5

可以看到,在R里,中文漢字一個(gè)字是一個(gè)字符寬度。同時(shí)注意區(qū)分和lengh函數(shù)的區(qū)別,length返回的是整個(gè)數(shù)據(jù)結(jié)構(gòu)的元素?cái)?shù)量

  • 提取或替換一個(gè)字符向量中的子串
    函數(shù)基本使用格式是substr(x , start ,stop ),可以截取部分子串,也可以替換。
> y <- "122267"
> 
> substr(y, 2, 4)
[1] "222"
> 
> substr(y,2,4 ) <- "ccc"
> 
> print(y)
[1] "1ccc67"

注意,這個(gè)替換是在原字符串進(jìn)行替換,且R索引從1開始。

  • 在字符中搜索某種模式
    使用格式為grep(pattern , x , ignore. case=FALSE, fixed=FALSE),ignore. case設(shè)置是否忽略大小寫,fixed=FALSE設(shè)置是否表示模式是一個(gè)正則表達(dá)式,暫時(shí)先不討論正則表達(dá)式。
> x <- "jakAnh"
> 
> y <- c("slA" , "djkaA")
> 
> grep("A", x, ignore.case = F, fixed = T)
[1] 1
> 
> grep("A", x, ignore.case = T, fixed = T)
[1] 1
Warning message:
In grep("A", x, ignore.case = T, fixed = T) :
  argument 'ignore.case = TRUE' will be ignored
> 
> grep("A", y, ignore.case = F, fixed = T)
[1] 1 2
> 
> grep("A", y, ignore.case = T, fixed = T)
[1] 1 2
Warning message:
In grep("A", y, ignore.case = T, fixed = T) :
  argument 'ignore.case = TRUE' will be ignored

注意,首先這個(gè)函數(shù)返回的是下標(biāo)索引,所以在取數(shù)據(jù)結(jié)構(gòu)子集時(shí)可以用。
再看,函數(shù)作用的數(shù)據(jù)結(jié)構(gòu)是向量,即使輸入一個(gè)字符串,而其中包含多個(gè)匹配上的模式,也只會(huì)返回1

  • 在字符中搜索模式, 并以文本replacement將其替換

使用格式sub(pattern ,replacement , x , ignore.case=FALSE,fixed=FALSE),后面兩個(gè)常用設(shè)置和上一個(gè)函數(shù)一樣,不多贅述。

> y <- "23777c"
> sub('7', 'M', y, ignore.case = T, fixed = T)
[1] "23M77c"
Warning message:
In sub("7", "M", y, ignore.case = T, fixed = T) :
  argument 'ignore.case = TRUE' will be ignored

注意,它只替換了匹配到的第一個(gè)字符。

  • 在split處分割字符向量中的元素
    使用格式strsplit(x , split ,fixed=FALSE),直接看例子,
> y <- "23777c"
> strsplit(y, "7", fixed = T)
[[1]]
[1] "23" ""   ""   "c" 

可以看到,是以7為分隔符,字符被分成了4個(gè)部分,其中兩個(gè)為空。

  • 連接字符串
    有兩個(gè)基礎(chǔ)函數(shù)可以使用paste (..., sep = " ", collapse = NULL, recycle0 = FALSE)paste0(..., collapse = NULL, recycle0 = FALSE),直接用幫助文檔的例子吧,要說差別,可能就是paste存在一個(gè)分隔符選項(xiàng)sep = " "吧。
> nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
> 
> month.abb  # 內(nèi)置數(shù)據(jù),就和letters一樣
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> paste(month.abb, "is the", nth, "month of the year.")
 [1] "Jan is the 1st month of the year."  "Feb is the 2nd month of the year." 
 [3] "Mar is the 3rd month of the year."  "Apr is the 4th month of the year." 
 [5] "May is the 5th month of the year."  "Jun is the 6th month of the year." 
 [7] "Jul is the 7th month of the year."  "Aug is the 8th month of the year." 
 [9] "Sep is the 9th month of the year."  "Oct is the 10th month of the year."
[11] "Nov is the 11th month of the year." "Dec is the 12th month of the year."
> library(stringr)
> library(Hmisc)

載入程輯包:‘Hmisc’

The following objects are masked from ‘package:dplyr’:

    src, summarize

The following objects are masked from ‘package:base’:

    format.pval, units

> 
> x <- 'apple'
> y <- 'BANANA'
> 
> toupper(x)
[1] "APPLE"
> tolower(y)
[1] "banana"
> capitalize(x)  # HMisc包函數(shù)
[1] "Apple"
> str_to_title(x) # stringr包函數(shù)
[1] "Apple"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容