Navin Manaswi
Navin Manaswi

Reputation: 992

Removing last consecutive digits

I have a column of alphanumeric data and i have to remove the last consecutive digits. It could be of any length.

Input:

dlxcp01 
dlcs8012
fg2fdes1

Desired Output:

dlxcp
dlcs
fg2fdes

As i have large dataset, a right code would do it better.

Upvotes: 0

Views: 48

Answers (2)

Stedy
Stedy

Reputation: 7469

Use the gsub() function:

text <- c('dlxcp01', 'dlcs8012', 'fg2fdes1')
gsub('[0-9]*$', "", text)
[1] "dlxcp"   "dlcs"    "fg2fdes"

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174796

Use sub function.

sub("[0-9]+$", "", x)

or

sub("[[:digit:]]+$", "", x)

Upvotes: 1

Related Questions