gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
其中pattern是要替換的字符,replacement是替換的字符,x是對(duì)應(yīng)的string或string vector。
舉例如下:
> x <- "R Tutorial"
> gsub("ut","ot",x)
[1] "R Totorial"
ignore.case表示是否忽視大小寫(xiě)。
舉例vector:
> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples" "PHP Examples" "HTML Examples"
還有其他的一些例子來(lái)靈活使用這個(gè)函數(shù),更詳細(xì)的說(shuō)明看下面的Ref就可以了。
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("\\d+","---",x)
> y
[1] "line ---: He is now --- years old, and weights ---lbs"
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("[[:lower:]]","-",x)
> y
[1] "---- 4322: H- -- --- 25 ----- ---, --- ------- 130---"