R 中的 gsub 异常
exception with gsub in R
我正在尝试用 "other" 替换所有字符串,但以下示例中以数字开头的字符串除外:
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"
有人可以帮帮我吗?提前致谢!
尝试
sub('^[^0-9].*', "other", strings)
#[1] "other" "5.test" "6.test" "other" "other"
我正在尝试用 "other" 替换所有字符串,但以下示例中以数字开头的字符串除外:
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"
有人可以帮帮我吗?提前致谢!
尝试
sub('^[^0-9].*', "other", strings)
#[1] "other" "5.test" "6.test" "other" "other"