1

I'm trying to remove the 0 that appears at the beginning of some observations for Zipcode in the following table:

enter image description here

I think the sub function is probably my best choice but I only want to do the replacement for observations that begin with 0, not all observations like the following does:

data_individual$Zipcode <-sub(".", "", data_individual$Zipcode)

Is there a way to condition this so it only removes the first character if the Zipcode starts with 0? Maybe grepl for those that begin with 0 and generate a dummy variable to use?

Sam Pickwick
  • 303
  • 1
  • 8
  • We cannot copy data from an image. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Feb 09 '21 at 02:03

1 Answers1

3

We can specify the ^0+ as pattern i.e. one or more 0s at the start (^) of the string instead of . (. in regex matches any character)

data_individual$Zipcode <- sub("^0+", "", data_individual$Zipcode)

Or with tidyverse

library(stringr)
data_individual$Zipcode <- str_remove(data_individual$Zipcode, "^0+")

Another option without regex would be to convert to numeric as numeric values doesn't support prefix 0 (assuming all zipcodes include only digits)

data_individual$Zipcode <- as.numeric(data_individual$Zipcode)
akrun
  • 874,273
  • 37
  • 540
  • 662