How to Add Leading Zeros to Numbers in R


Often you may want to add leading zeros to each number in a vector in R.

Here are the most common ways to do so:

Method 1: Add Leading Zeros to Numbers Using paste0()

df$ID <- paste0("00", df$ID)

This particular example uses the paste0() function concatenate two leading zeros to each value in the ID column of the data frame named df.

Method 2: Add Leading Zeros to Numbers Using str_pad()

library(stringr)

df$ID <- str_pad(df$ID, 2, pad = "0")

This particular example uses the str_pad() function from the stringr package to add two leading zeros to each value in the ID column of the data frame named df.

Method 3: Add Leading Zeros to Numbers Using sprintf()

df$ID <- sprintf("%05s", df$ID)

This particular example uses the sprintf() function add enough leading zeros to each value in the ID column of the data frame named df so that the final width of each value is 5.

This method is particularly useful when you want each value in a particular column of a data frame to have the same width.

The following examples show how to use each method in practice with the following data frame that contains one column named ID to represent employee ID values at a company and another column named sales that represents the total sales made by each employee:

#create data frame
df <- data.frame(ID=c(934, 455, 884, 229, 304, 307, 352, 405, 454, 882),
                 sales=c(29, 49, 34, 56, 30, 76, 56, 80, 88, 32))

#view data frame
df

    ID sales
1  934    29
2  455    49
3  884    34
4  229    56
5  304    30
6  307    76
7  352    56
8  405    80
9  454    88
10 882    32

Let’s jump in!

Example 1: Add Leading Zeros to Numbers Using paste0()

We can use the following syntax with the paste0() function to add two leading zeros to each number in the ID column of the data frame:

#add two leading zeros to each number in ID column
df$ID <- paste0("00", df$ID)

#view updated data frame
df

      ID sales
1  00934    29
2  00455    49
3  00884    34
4  00229    56
5  00304    30
6  00307    76
7  00352    56
8  00405    80
9  00454    88
10 00882    32

Notice that two leading zeros have been added to each number in the ID column of the data frame.

Example 2: Add Leading Zeros to Numbers Using str_pad()

We can use the following syntax with the str_pad() function from the stringr package to add two leading zeros to each number in the ID column of the data frame:

library(stringr)

#add two leading zeros to each number in ID column
df$ID <- str_pad(df$ID, 2, pad = "0")

#view updated data frame
df

      ID sales
1  00934    29
2  00455    49
3  00884    34
4  00229    56
5  00304    30
6  00307    76
7  00352    56
8  00405    80
9  00454    88
10 00882    32

Notice that two leading zeros have been added to each number in the ID column of the data frame.

Example 3: Add Leading Zeros to Numbers Using sprintf()

We can use the following syntax with the sprintf() function to add enough leading zeros to each number in the ID column of the data frame to make each number have a final width of 5:

#add enough leading zeros to make each number have a width of 5
df$ID <- sprintf("%05s", df$ID)

#view updated data frame
df

      ID sales
1  00934    29
2  00455    49
3  00884    34
4  00229    56
5  00304    30
6  00307    76
7  00352    56
8  00405    80
9  00454    88
10 00882    32

Notice that two leading zeros have been added to each number in the ID column of the data frame, which gives each number a final width of 5.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Convert a Vector to String in R
How to Convert Strings to Lowercase in R
How to Perform Partial String Matching in R

Leave a Reply

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