您当前的位置:首页 > r

R语言 字符串处理函数

摘自:http://www.biostatistic.net/thread-195-1-1.html
Gbolinux系统宝典
用于字符串分割的函数:

strsplit('123abcdefgabcdef','ab')

[[1]]
[1"123"  "cdefg" "cdef" 

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

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

#计算字符串的字符数:
nchar()

#字符串截取:
substr(x, startstop)
substring(text,first, last = 1000000)
substr(x, startstop)<- value
substring(text,first, last = 1000000)<- value
###########例子说明
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


###########


#字符串替换及大小写转换:
chartr(old, new,x)
tolower(x)
toupper(x)
casefold(x,upper = FALSE


字符完全匹配
grep()
字符不完全匹配
agrep()
字符替换
gsub()
#以上这些函数均可以通过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 thedetails of the pattern
    specification.

    'glob2rx' to turnwildcard matches into regular expressions.

    'agrep' for approximatematching.

    'tolower''toupper' and 'chartr' for character translations.
    'charmatch''pmatch''match''apropos' usesregexps and has
    niceexamples.


沪ICP备10206494号-4