R函数-字符串操作
?字符串操作
#++++++++++++++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++++++++++++++ #R語言+++++++++字符串處理函數(shù)+++++++++ #內(nèi)容概覽: #盡管R是一門數(shù)值向量和矩陣為核心的統(tǒng)計語言,但字符串同樣極為重要。 #從醫(yī)療研究數(shù)據(jù)里的出生日期到文本挖掘的應(yīng)用,字符串數(shù)據(jù)在R程序中的使用頻率非常高。 #++++++++++++++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++++++++++++++ #------------------------ #字符型向量 character #字符數(shù) nchar #取子串 substr #把對象用格式轉(zhuǎn)換為字符串 format,formatC #連接或拆分 paste,strsplit #字符串匹配 charmatch,pmatch #模式匹配與替換 grep,sub,gsub #------------------------ #字符串連接的函數(shù): #paste(..., sep = " ", collapse = NULL) #paste()函數(shù)用于字符串連接,其中sep負責兩組字符串間的連接;collapse負責一組字符串內(nèi)部的連接。 paste() #------------------------ #字符串分割的函數(shù): #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE) strsplit() #strsplit()函數(shù)用于字符串分割,其中split是分割參數(shù)。所得結(jié)果以默認list形式展示。 myresult <- strsplit('123abcdefgabcdef','ab') #myresult <- strsplit('123abcdefgabcdef',split='ab') myresult #[[1]] #[1] "123" "cdefg" "cdef" class(myresult) #------------------------ #計算字符串的字符數(shù): nchar() #nchar()返回字符串的長度。 nchar("abc") nchar(NA) #缺失值長度 nchar(Inf) #無限長度值 nchar(NULL) #NULL情況的結(jié)果 nchar("") # "" 這種情況結(jié)果 length(nchar("")) # "" 長度是有值的,就是0 length(nchar(NULL)) # NULL長度是沒有值的 #------------------------ #字符串截取: substr(x, start, stop) substring(text, first, last = 1000000) #substr()函數(shù)和substring()函數(shù)是截取字符串最常用的函數(shù),兩個函數(shù)功能方面是一樣的,只是其中參數(shù)設(shè)置不同。 #substr()函數(shù):必須設(shè)置參數(shù)start和stop,如果缺少將出錯。 #substring()函數(shù):可以只設(shè)置first參數(shù),last參數(shù)若不設(shè)置,則默認為1000000L,通常是指字符串的最大長度。 substr(x, start, stop) <- value #通過value值替換x中的部分 substring(text, first, last = 1000000) <- value #通過value值替換x中的部分 ###########例子說明 substr("abcdef",2,4) substring("abcdef",1:6,1:6) # strsplit is more efficient ... substr(rep("abcdef",4),1:4,4:5) x <- c("asfef", "qwerty", yuiop[", "b", "stuff.blah.yech") substr(x, 2, 5) substring(x, 2, 4:6) substring(x, 2) <- c("..", "+++") x #------------------------ #字符串替換函數(shù): chartr(old, new, x) #------------------------ #大小寫轉(zhuǎn)換函數(shù): tolower(x) toupper(x) casefold(x, upper = FALSE) #------------------------ #字符完全匹配 grep() #字符不完全匹配 agrep() #字符替換 gsub() #以上這些函數(shù)均可以通過perl=TRUE來使用正則表達式。 grep(pattern, x, ignore.case = FALSE, extended = TRUE,perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE)sub(pattern, replacement, x,ignore.case = FALSE, extended = TRUE, perl = FALSE,fixed = FALSE, useBytes = FALSE)gsub(pattern, replacement, x,ignore.case = FALSE, extended = TRUE, perl = FALSE,fixed = FALSE, useBytes = FALSE)regexpr(pattern, text, ignore.case = FALSE, extended = TRUE,perl = FALSE, fixed = FALSE, useBytes = FALSE)gregexpr(pattern, text, ignore.case = FALSE, extended = TRUE,perl = FALSE, fixed = FALSE, useBytes = 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. #------------------------?
轉(zhuǎn)載于:https://www.cnblogs.com/cloudtj/articles/5159060.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
- 上一篇: FZU 2150 Fire Game b
- 下一篇: HDU 1042 N!