3

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    `as.numeric(gsub('\\D+', '', numbers))` – Sotos Sep 01 '16 at 10:50
  • @Sotos for some reason it's not showing me numbers that start with 0 in th e output – Mattematics Sep 01 '16 at 10:59
  • It shows the numbers via scientific notation and also when we convert to `numeric` It is expected to not put the leading 0. I get: `[1] 9.722642e+10 8.152646e+08 4.902023e+12 7.814828e+08 6.643420e+09 6.041873e+07` – Sotos Sep 01 '16 at 11:01

2 Answers2

1

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"
Sotos
  • 51,121
  • 6
  • 32
  • 66
0

([0-9]+).*$ puts a number sequence until the first non-number into \\1. However, you want:

numbers <- readLines(n=6)
972 2 6424979
81|5264627
49-0202-2801986
07.81.48.27.89
0664/3420034
06041 - 8728
as.numeric(gsub("\\D", "", numbers))

This replaces all non-numbers by nothing.

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • and [this](http://stackoverflow.com/questions/5352099/how-to-disable-scientific-notation-in-r) if the scientific notation does indeed bother the OP... – Sotos Sep 01 '16 at 10:57