我正在尝试用"其他"替换所有字符串,除了以下示例中以数字开头的字符串:
strings <- c("test.5","5.test","6.test","test","test")
我发现下面的代码只替换以数字开头的字符串:
gsub("^[0-9].+", "other", strings)
"test.5" "other" "other" "test" "test"
但是,我对如何反转语句感到困惑,因此除了这些字符串之外的所有内容都会被替换。
期望的答案是
"other" "5.test" "6.test" "other" "other"
有人可以帮帮我吗?提前致谢!
答案 0 :(得分:2)
尝试
sub('^[^0-9].*', "other", strings)
#[1] "other" "5.test" "6.test" "other" "other"