I need to extract a nonnegative number from a string or return negative number if a number was not extracted.
For extracting the number I found the following way:
> grep("^[0-9.]+","1234.1234alsk",value=TRUE)
[1] "1234.1234alsk"
If the given string is not a number, then empty string is returned:
> grep("^[0-9.]+","",value=TRUE)
character(0)
Now I would like to replace the empty string with some proxy number, like 0
or -1
using the following kind of function:
> sub("^$","-1","")
[1] "-1"
However, If I apply that function to an empty string of character(0)
I do not get the desired result:
> sub("^$","-1",grep("^[0-9.]+","",value=TRUE))
character(0)
The problem is that grep
returns character(0)
and not ""
. Then because sub
works differently with character(0)
and ""
, I do not get the desired value "-1"
but unchanged character(0)
. As a result those values will be dropped in the following query:
> v <- c("0","","1","2")
> as.numeric(sub("^$","-1",grep("^[0-9.]+",v,value=TRUE)))
[1] 0 1 2
How could I do the above kind of conversion using one-liner?