0

Possible duplicate: 1 2

I read the above discussions.

I want to get all numerical characters from alphanumerical string using R?

My Code:

   > y <- c()
   > x <- c("wXYz04516", "XYz24060", "AB04512", "wCz04110", "wXYz04514", "wXYz04110")
   > for (i in 1:length(x)){
   +       y <- c(as.numeric(gsub("[a-zA-Z]", "", x[i])),y)
   + }
   > print (y)

[1]  4110  4514  4110  4512 24060  4516

Here it outputs the all numerical charters, but fail to get starting number zero ("0")

   The output omits starting Zero ("0") digit in case of 4110,  4514,  4110, 4512, and  4516.

How can I get digit zero included before the numbers?

Community
  • 1
  • 1
Issac
  • 47
  • 1
  • 1
  • 6
  • 1
    Yeah, as @RichardScriven is working on I think, if you want the leading zero, you'll need to leave them as characters. – joran Jan 29 '15 at 23:31
  • @RichardScriven and @ Joran thanks ... Thanks for letting me know about leading zero . – Issac Jan 29 '15 at 23:36

2 Answers2

4

Leading zeroes are not allowed on whole numeric values. So to have the leading zeros, you'll have to leave them as character. You can, however, print them without quotes if you want.

x <- c("wXYz04516", "XYz24060", "AB04512", "wCz04110", "wXYz04514")

gsub("\\D+", "", x)
# [1] "04516" "24060" "04512" "04110" "04514"

as.numeric(gsub("\\D+", "", x))
# [1]  4516 24060  4512  4110  4514

print(gsub("\\D+", "", x), quote = FALSE)
# [1] 04516 24060 04512 04110 04514

So the last one looks like a numeric, but is actually a character.

Side note: gsub() and as.numeric() are both vectorized functions, so there's also no need for a for() loop in this operation.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
0

If you want the leading zeroes, you will need to create a character vector instead of numeric one, so change as.numeric to as.character.

AlecBrooks
  • 524
  • 4
  • 12