I have a vector of data, a similified version is below:
x <- c("1234123xcv?","12341534xxx.","hello","goodbye")
What I would like to do is have it return the following:
"1234123" "12341534" "" ""
I know I can do something like this, where I manually specify each upper/lower case letters and the few special characters that I'm aware of:
grep("[A-Za-z\\?\\.]",x,value=TRUE)
But I don't know what "else" is in the field that's not necessarily a number. (and can't look through it manually, because it's too large)
With that in mind my question is: Is there a way to specify that you want ONLY numbers to be returned in gsub()
?
gsub("\\D","",x) # yada yada yada
Inside the brackets, ^
means not. So, this says replace whatever is not a number with ""
> gsub("[^0-9]", "", x)
[1] "1234123" "12341534" "" ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With