【R語言】常用的字符串處理函數 【待更新】


1.

 

#字符串連接:
paste() #paste(..., sep = " ", collapse = NULL)

#字符串分割:
strsplit() #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)

例如.

strsplit('123abcdefgabcdef','ab')
[[1]]
[1"123"   "cdefg" "cdef" 


#計算字符串的字符數:
nchar()

#字符串截取:
substr(xstartstop)
substring(textfirstlast = 1000000)
substr(xstartstop) <- value
substring(textfirstlast = 1000000) <- value

#字符串替換及大小寫轉換:
chartr(oldnewx)
tolower(x)
toupper(x)
casefold(xupper = FALSE

 

#匹配相關的函數:

字符完全匹配
grep()
字符不完全匹配
agrep()
字符替換
gsub()
#以上這些函數均可以通過perl=TRUE來使用正則表達式。
     grep(patternxignore.case = FALSEextended = TRUE,
          perl = FALSEvalue = FALSEfixed = FALSEuseBytes = FALSE)

     sub(patternreplacementx,
         ignore.case = FALSEextended = TRUEperl = FALSE,
         fixed = FALSEuseBytes = FALSE)

     gsub(patternreplacementx,
          ignore.case = FALSEextended = TRUEperl = FALSE,
          fixed = FALSEuseBytes = FALSE)

     regexpr(patterntextignore.case = FALSEextended = TRUE,
             perl = FALSEfixed = FALSEuseBytes = FALSE)

     gregexpr(patterntextignore.case = FALSEextended = TRUE,
              perl = FALSEfixed = FALSEuseBytes = FALSE)
See Also:

     regular expression (aka 'regexp'for the details of the pattern
     specification.

     'glob2rx' to turn wildcard matches into regular expressions.

     'agrep' for approximate matching.

     'tolower''toupper' and 'chartr' for character translations.
     'charmatch''pmatch''match'. 'apropos' uses regexps and has
     nice examples.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM