ashcrashbegash
ashcrashbegash

Reputation: 271

Remove middle 0 in string in R

I am trying to remove a zero within a string in R. I have a column in a frame that have to match with another, but one has a 0 one doesn't. For instance,

A01 vs A1

It gets tricky when I have A10 ... A12.

I have searched, but can't figure out how to do this without copy and paste in the excel file.

Upvotes: 5

Views: 388

Answers (1)

akrun
akrun

Reputation: 886938

We can use sub

sub("A0", "A", c("A01", "A10"))
#[1] "A1"  "A10"

or to be more exact

sub("([A-Z])0(.*)", "\\1\\2", c("A01", "A10"))
#[1] "A1"  "A10"

Upvotes: 4

Related Questions