Reputation: 47
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?
Upvotes: 0
Views: 2579
Reputation: 99391
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.
Upvotes: 4
Reputation: 520
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
.
Upvotes: 0