Mattematics - 8 months ago 57

R Question

I have strings with numbers as follow:

`972 2 6424979`

81|5264627

49-0202-2801986

07.81.48.27.89

0664/3420034

06041 - 8728

and would like to get an output like:

`97226424979`

815264627

4902022801986

0781482789

06643420034

060418728

I tried using:

`as.numeric(gsub("([0-9]+).*$", "\\1", numbers))`

but the numbers are separate in the output.

Answer

To get your exact output,

```
#to avoid scientific notation
options(scipen=999)
#find which have leading 0
ind <- which(substring(x, 1, 1) == 0)
y <- as.numeric(gsub("\\D", "", numbers))
y[ind] <- paste0('0', y[ind])
y
#[1] "97226424979" "815264627" "4902022801986" "0781482789" "06643420034" "060418728"
```