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,130 points
3,333 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 darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
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
188 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 4, 2018 in Data Analytics by Kalgi
• 40,480 points
49 views
0 votes
1 answer

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

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

answered Dec 6, 2018 in Data Analytics by Maverick
• 10,040 points
102 views
0 votes
0 answers

How to add custom labels to charts in R?

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

Jul 3 in Data Analytics by Prerana
12 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
47 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,550 points
63 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,550 points
127 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

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

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
103 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 darklord
• 6,140 points
104 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 in Data Analytics by anonymous
1,328 views