gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
其中pattern是要替換的字符,replacement是替換的字符,x是對應(yīng)的string或string vector泉唁。
舉例如下:
> x <- "R Tutorial"
> gsub("ut","ot",x)
[1] "R Totorial"
ignore.case表示是否忽視大小寫。
舉例vector:
> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples" "PHP Examples" "HTML Examples"
還有其他的一些例子來靈活使用這個函數(shù)岳链,更詳細(xì)的說明看下面的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---"