Remove Leading and Trailing Zeros in R (5 Examples)

 

In this R tutorial you’ll learn how to delete zeros at the beginning and at the end of a value.

The tutorial consists of the following content:

Let’s dive into it…

 

Creation of Exemplifying Data

I’ll use the following data as basement for this R programming language tutorial:

my_values <- c("000123", "055", "01010", "22200")  # Create example data
my_values                                          # Print example data
# [1] "000123" "055"    "01010"  "22200"

The previous output of the RStudio console illustrates the structure of our example data: It’s a vector containing several character strings. Each character string is a number with leading and/or trailing zeros.

 

Example 1: Delete Leading Zeros Using sub() Function

In this example, I’ll illustrate how to avoid leading zeros using the sub function in R:

my_values_new1 <- sub("^0+", "", my_values)        # Apply sub function
my_values_new1                                     # Print updated data
# [1] "123"   "55"    "1010"  "22200"

Have a look at the previous output of the RStudio console: It shows our vector of character strings without zeros at the start.

 

Example 2: Delete Leading Zeros Using str_remove() Function of stringr Package

In Example 2, I’ll show how to apply the str_remove function of the stringr package to delete leading zeros in R.

In case we want to use the functions of the stringr package, we first need to install and load stringr to RStudio:

install.packages("stringr")                        # Install stringr package
library("stringr")                                 # Load stringr package

Now, we can use the str_remove function to extract all zeros at the front of our data:

my_values_new2 <- str_remove(my_values, "^0+")     # Apply str_remove function
my_values_new2                                     # Print updated data
# [1] "123"   "55"    "1010"  "22200"

The output is exactly the same as in Example 1.

 

Example 3: Delete Leading Zeros & Convert Values to Numeric Using as.numeric() Function

The following R programming syntax shows how to get rid of leading zeros using the as.numeric function in R.

my_values_new3 <- as.numeric(my_values)            # Apply as.numeric function
my_values_new3                                     # Print updated data
# [1]   123    55  1010 22200

The numbers shown in the previous RStudio console output are the same as in the previous examples.

However, please note that the data class was converted from character to numeric.

 

Example 4: Delete Trailing Zeros Using sub() Function

In this example, I’ll explain how to remove trailing zeros from our data using the sub function:

my_values_new4 <- sub("0+$", "", my_values)        # Apply sub function
my_values_new4                                     # Print updated data
# [1] "000123" "055"    "0101"   "222"

This time, the zeros at the beginning of our character strings have been kept, but the zeros at the end of the character strings were deleted.

 

Example 5: Delete Trailing Zeros Using str_remove() Function of stringr Package

We can also use the str_remove function of the stringr package to remove trailing zeros:

my_values_new5 <- str_remove(my_values, "0+$")     # Apply str_remove function
my_values_new5                                     # Print updated data
# [1] "000123" "055"    "0101"   "222"

Looks good!

 

Video & Further Resources

Do you need further information on the R programming code of the present tutorial? Then you might want to watch the following video of my YouTube channel. I’m illustrating the content of the present article in the video:

 

The YouTube video will be added soon.

 

In addition to the video, you might want to read some of the related tutorials of my homepage. I have released numerous articles about data manipulation already.

 

This tutorial illustrated how to remove one or multiple zeros at the beginning and at the end of a character string in R programming. Please let me know in the comments, in case you have any additional comments and/or questions.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top