ZMacarozzi
ZMacarozzi

Reputation: 717

exception with gsub in R

I am trying to replace all strings with "other", EXCEPT the ones starting with numbers in the following example:

strings <- c("test.5","5.test","6.test","test","test")

I found that the code below replaces only the strings starting with numbers:

gsub("^[0-9].+", "other", strings)
"test.5" "other"  "other"  "test"   "test" 

However, I am really confused about how to reverse the statement, so that everything EXCEPT these strings is replaced.

Desired answer would be

 "other" "5.test"  "6.test"  "other"   "other"

Can someone help me out? Thanks in advance!

Upvotes: 3

Views: 716

Answers (1)

akrun
akrun

Reputation: 887028

Try

 sub('^[^0-9].*', "other", strings)
 #[1] "other"  "5.test" "6.test" "other"  "other" 

Upvotes: 2

Related Questions