0

I have a data frame as follows (part of a larger set):

data frame

for the column raw$Zipcode I need to remove the two 00 before the Zipcode number for Swedish zipcodes (preferably through a function). I am very new to R and have found gsub and strsplit but can't seem to make it work:

raw2 <- unlist(strsplit(raw$ZipCode, split="00", fixed=TRUE))[2]

The zeroes are characters as other countries in the data set have letters. How can I remove the first two zeroes in all cases of the two first character letters being zeroes in a column?

Vishal A.
  • 1,373
  • 8
  • 19
Brune
  • 15
  • 3

2 Answers2

0
v <- c("00345", "00045", "12345", "12005")

sub("^0{2}", "", v)

# [1] "345"   "045"   "12345" "12005"
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22
0

There are multiple ways of doing this:

  1. Using as.numeric on a column of your choice.
raw$Zipcode <- as.numeric(raw$Zipcode)
  1. If you want it to be a character then you can use stringr package.
library(stringr)
raw$Zipcode <- str_replace(raw$Zipcode, "^0+" ,"")
  1. There is another function called str_remove in stringr package.
raw$Zipcode <- str_remove(raw$Zipcode, "^0+")
  1. You can also use sub from base R.
raw$Zipcode <- sub("^0+", "", raw$Zipcode)

But if you want to remove n number of leading zeroes, replace + with {n} to remove them.

For instance to remove two 0's use sub("^0{2}", "", raw$Zipcode).

Vishal A.
  • 1,373
  • 8
  • 19
  • Thanks alot! So the ansswer is to substitute the 0 with a nothing "" in the function: sub("^0{2}", "", raw$Zipcode). Also if you will be so kind, what is the meaning of the "^" – Brune Jan 28 '22 at 08:22
  • Yes! If that has solved your problem, please consider accepting as an answer by clicking the grey tick on the left of this answer. – Vishal A. Jan 28 '22 at 08:29