进行替换
# example data
test_sentences <- c("The quick brown fox quickly", "jumps over the lazy dog")
让棕色的狐狸变成红色:
sub("brown","red", test_sentences)
#[1] "The quick red fox quickly" "jumps over the lazy dog"
现在,让我们来做 fast
fox act fastly
。这不会这样做:
sub("quick", "fast", test_sentences)
#[1] "The fast red fox quickly" "jumps over the lazy dog"
sub
只是第一个可用的替代品,我们需要 gsub
进行全局更换 :
gsub("quick", "fast", test_sentences)
#[1] "The fast red fox fastly" "jumps over the lazy dog"
有关更多示例,请参阅通过替换修改字符串 。