R語言處理字串

2021-06-10 11:03:25 字數 2479 閱讀 6487

用於字串分割的函式:

如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,

start,

stop)

substring(

text, first, last =

1000000)

substr(x,

start,

stop)<- 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')forthe details of the pattern

specification.

'glob2rx' to turn wildcard matches into regularexpressions.

'agrep'for

'tolower',

'toupper'and

'chartr'forcharacter translations.

'charmatch',

'pmatch',

'match'.

'apropos' uses regexps and has

niceexamples.

字串處理函式 R語言

用於字串分割的函式 如strsplit 123abcdefgabcdef ab 1 1 123 cdefg cdef 字串連線 paste paste sep collapse null 字串分割 strsplit strsplit x,split,extended true,fixed false...

R語言 字串處理函式

r語言中字串處理函式 來自base包 函式說 明 nchar x 計算x中的字元數量 substr 提取或替換乙個字元向量中的子串 grep 在字串中匹配某種模式 sub 在字串中搜尋模式,並以另乙個文字替換 strsplit 分割字串 toupper 大寫轉換 tolower 小寫轉換 1 nch...

R字串處理

r 字串處理 1 字串連線 paste paste sep collapse null paste a b c sep 1 a b c 2 字串分割 strsplit strsplit x,split,extended true,fixed false,perl false strsplit a b...