R的替换sub和gsub

sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
 
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
 
> text<-c("Hello Adam!\nHello Ava!")
 
> sub(pattern="Adam|Ava",replacement="world", text) #只做一次替换(不管有几次匹配)
[1] "Hello world!\nHello Ava!"
 
> gsub(pattern="Adam|Ava",replacement="world", text) #满足条件的匹配都做替换
[1] "Hello world!\nHello world!"
 
> text #原字符串并没有改变
[1] "Hello Adam!\nHello Ava!"

  

posted @ 2015-03-19 23:11  智能先行者  阅读(2067)  评论(0编辑  收藏  举报