How to add leading zeros in R

0 votes

Consider the following code:

a <- seq(1,101,25)
b <- paste("sequence", 1:length(a), sep = "_")

produces the following output:

"sequence_1"  "sequence_26"  "sequence_51"  "sequence_76"  "sequence_101"

I want to have the same width of all values i.e. I want to fill the values with zeros as follows:

I'd like to have the same width of all values which means for me to fill the values with zeros like this:

"sequence_001"  "sequence_026"  "sequence_051"  "sequence_076"  "sequence_101"

How do I do that?

Apr 30, 2018 in Data Analytics by DataKing99
• 8,240 points
24,018 views

1 answer to this question.

0 votes

There are several solutions to this.

One of them is to use sprintf. This uses C style formatting codes embedded in a character string to indicate the format of any other arguments passed to it.

 For example, the formatting code %3dmeans format a number as an integer of width 3:

a <- seq(1,101,25)
sprintf("sequence_%03d", a)
[1] "sequence_001" "sequence_026" "sequence_051" "sequence_076" "sequence_101"

Another option is formatC and paste:

paste("sequence", formatC(a, width=3, flag="0"), sep="_")
[1] "sequence_001" "sequence_026" "sequence_051" "sequence_076" "sequence_101"
answered Apr 30, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

+1 vote
1 answer

How to add videos in a shiny R dashboard?

Its pretty simple, try this: server.r library(shiny) shinyServer(function(input, output, session) ...READ MORE

answered Dec 4, 2018 in Data Analytics by Haseeb
3,685 views
0 votes
2 answers

How to add sliderbars in a shiny R dashboard?

To add slider bar and slider range ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,360 points
1,157 views
+1 vote
1 answer

How to add grid in ggsurvplot without changing theme in R?

Try something like : j <- ggsurvplot( ...READ MORE

answered Dec 7, 2018 in Data Analytics by Maverick
• 10,840 points
1,345 views
0 votes
0 answers

How to add custom labels to charts in R?

How to add custom labels to charts ...READ MORE

Jul 4, 2019 in Data Analytics by Prerana
659 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
773 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
849 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,557 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
864 views
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by Sahiti
• 6,370 points
3,547 views
0 votes
2 answers

How to count unique values in R?

You can try this way, as.data.frame(v) %>% count(v) READ MORE

answered Aug 8, 2019 in Data Analytics by anonymous
6,290 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP