0

I have a variable with class "character", for example:

class(itc$`Mobile-cellular telephone subscriptions`)
[1] "character"

(itc$`Mobile-cellular telephone subscriptions`)
[1] "19'709'038"     "3'400'955"      "43'227'643"     "71'336"         "13'884'532" 

When I use as.numeric all the values becomes NA.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Zina Jg
  • 1
  • 3
  • either split them first or remove the delimiter completely...whichever you need – Sotos Jul 27 '17 at 11:24
  • Here is an example using the `stringr` library: `as.numeric(str_replace_all("19'709'038", "'", ""))` – roarkz Jul 27 '17 at 11:28
  • Or for the whole vector you could do: `cellphone <- c("19'709'038", "3'400'955", "43'227'643", "71'336")` `sapply(cellphone, function(x) unname(as.numeric(str_replace_all(x, "'", ""))))` – roarkz Jul 27 '17 at 11:33
  • The `destring()` function extracts only numeric values from a string. Might be useful. https://www.rdocumentation.org/packages/taRifx/versions/1.0.6/topics/destring – Andrew Brēza Jul 27 '17 at 11:35

1 Answers1

0

We can use str_extract

library(stringr)
sapply(str_extract_all(itc$`Mobile-cellular telephone subscriptions`, '[0-9.]+'), as.numeric)
akrun
  • 874,273
  • 37
  • 540
  • 662