Achal Neupane
Achal Neupane

Reputation: 5729

remove leading zero from numbers without decimal in R vector

Say I have a vector myvec <- c("00890","0891","Apple-20","00-china-usa","0123Belgium"). I want to remove all the zeros before numeric values in this vector and not from alpha-numeric and get the result as shown below.

result

"890", "891", "Apple-20","00-china-usa","0123Belgium"

Upvotes: 3

Views: 1855

Answers (3)

akrun
akrun

Reputation: 887991

We could do this with sub. We match strings that start (^) with one or more zeros followed by one or more numbers ([0-9]+) that we capture as a group (i.e. inside the (..)) until the end ($) of the string. In the replacement, we use the backreference (\\1) to replace with the captured group. Note that this will not touch the leading 0's in strings that have character suffix. i.e. 0123Belgium in this example.

sub("^0+([0-9]+)$", "\\1", myvec) 
#[1] "890"          "891"          "Apple-20"     "00-china-usa" "0123Belgium" 

Or use the ^ to match only numeric elements and not any alphabets.

sub("^0+([^[:alpha:]]+)$", "\\1", myvec) 

Testing with another vector to check whether this answer fails or not

sub("^0+([0-9]+)$", "\\1", myvec1) 
#[1] "8090"                "10"                  "00-china-012Belgium" "012C001"  

data

myvec <- c("00890","0891","Apple-20","00-china-usa","0123Belgium")
myvec1 <- c("008090", "010", "00-china-012Belgium", "012C001")

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 389355

Another alternative , we can convert the elements in myvec to numeric and the one which are alphanumeric are kept unchanged.

ifelse(is.na(as.numeric(myvec)), myvec, as.numeric(myvec))

#[1] "890"          "891"          "Apple-20"     "00-china-usa" "0123Belgium" 
#Warning messages:
#1: In ifelse(is.na(as.numeric(myvec)), myvec, as.numeric(myvec)) :
#NAs introduced by coercion
#2: In ifelse(is.na(as.numeric(myvec)), myvec, as.numeric(myvec)) :
#NAs introduced by coercion

Upvotes: 1

JeremyS
JeremyS

Reputation: 3525

You can do it with gsub and regular expressions. Putting the [1-9] in brackets allows you to select it to remain in the output using the \\1

gsub("^0+([1-9])","\\1",myvec)

edit: actually, that cuts the 0 from 0123Belgium, this works:

ifelse(grepl("[A-z]",myvec),myvec,gsub("^0+([1-9])","\\1",myvec))

Upvotes: 3

Related Questions