R之字串相關

2021-06-06 17:36:21 字數 2583 閱讀 1269

1.strsplit(m,n)  分割字串:將字串m,在含有字元n的地方進行分割

如:strsplit("abcdef","e"):  

結果為:

[[1]]

[1] "abcd" "f" 

strsplit(c("ab","cde","mnd"),"e")  結果為:

[[1]]

[1] "ab"

[[2]]

[1] "cd"

[[3]]

[1] "mnd"

2.substr(m,index1,length):擷取字串m,從index1索引處擷取長度為length的字串;

> substr("abcde",1,3)

[1] "abc"

#字串連線:

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

#字串替換及大小寫轉換:

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

ofthe

pattern

specification.

'glob2rx'

toturn

wildcard

matches

into

regular

expressions.

'agrep'

for

matching.

'tolower'

, 'toupper'

and'chartr'

for

character

translations.

'charmatch'

, 'pmatch'

, 'match'

.'apropos'

uses

regexps

andhas

nice

examples.

R之字串相關

1.strsplit m,n 分割字串 將字串m,在含有字元n的地方進行分割 如 strsplit abcdef e 結果為 1 1 abcd f strsplit c ab cde mnd e 結果為 1 1 ab 2 1 cd 3 1 mnd 2.substr m,index1,length 擷...

R語言 字串

在r中字串出現的地方要加引號 nchar函式可以返回字串的長度,空格也算乙個字串 length函式返回向量內字串的個數 paste可以將多個字串合併為乙個,在此函式裡可以設定sep函式設定分隔符 substr用於提取字串,函式引數分別為 乙個原始的字串,乙個起始點,乙個結束點,函式返回起始點和結束點...

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...