字串處理函式 R語言

2021-07-02 14:38:35 字數 2350 閱讀 9059

用於字串分割的函式:

如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') for

the details of the pattern

specification.

'glob2rx' to turn wildcard matches into regular expressions.

'agrep'

forfor

character translations.

'charmatch', 'pmatch', 'match'. 'apropos' uses regexps and has

nice examples.

暫時就這麼多吧。找到以後再貼上上。

有知道的朋友可以跟帖發上

**:

R語言 字串處理函式

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

R語言處理字串

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

R語言中的字串處理函式

儘管r是一門以數值向量和矩陣為核心的統計語言,但字串同樣極為重要。從醫療研究資料裡的出生日期到文字挖掘的應用,字串資料在r程式中使用的頻率非常高。r語言提供了很多字串操作函式,本文僅簡要以下幾種常用的字串函式。字串分割函式 strsplit 字串連線函式 paste 計算字串長度 nchar 字串擷...