進行替換
# 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"
有關更多示例,請參閱通過替換修改字串 。