0

I have a column that contains alphanumeric data. I would like to remove the alphabetic character only if it's in the beginning of the string.

Example of what the data looks like: Z999999999 12ABC 123456AB7

What I would like the data to look like: 999999999 12ABC 123456AB7

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Alyssa
  • 27
  • 1

2 Answers2

3

You need to use regular expression:

sub(pattern = "^[a-zA-Z]",replacement = "",x = "Z999999999 12ABC 123456AB7")

Read:

https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
yusuzech
  • 5,896
  • 1
  • 18
  • 33
2
library(stringr)
x <- 'Z999999999 12ABC 123456AB7'
str_replace(x, '^[a-zA-Z]','') # find a leading letter and replace it with nothing
Tony Ladson
  • 3,539
  • 1
  • 23
  • 30