简体   繁体   中英

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"

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))

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 (ie 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. ie 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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM