Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:17:24+00:00 2026-06-01T20:17:24+00:00

I need to convert a vector of single digit numbers (e.g. c(1, 2, 3,

  • 0

I need to convert a vector of single digit numbers (e.g. c(1, 2, 3, 45)) to a character vector where the length of each item is two, and it is stuffed with a zero before the actual number (e.g. c("01", "02", "03", "45").

I could swear that I’ve done this before in R with some clever function but I can’t for the life of me find it or remember what it is… I don’t think it’s format(), but I might be amazed.

TL;DR:

What function do I need to convert this:

c(1, 2, 3, 45)

To this:

c("01", "02", "03", "45")

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:17:25+00:00Added an answer on June 1, 2026 at 8:17 pm

    sprintf with a padding character:

    sprintf("%02d",c(1,2,3,45))
    

    You could also use formatC, it uses basically the same set of formatting conventions (from the C standard library):

    formatC(c(1,2,3,45),flag=0,width=2)
    

    Also, a near-duplicate (sorry, didn’t see it before I answered):

    Format number as fixed width, with leading zeros

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (void*) buffer that I need to convert to std::vector<unsigned char> before
I have problem i need to convert from my Array structure to std::vector<int> ...
I need to store some names into a vector and then convert that data
I need to store some names in Vector and then I want to convert
I need to convert a Decimal Number to a Binary Vector For example, Something
I have a vector of strings. Need help figuring out how to convert it
I need to convert latitude/longitude coordinates into Easting/Northing coordinates in the Alberta 10 TM
I need to convert a named instance of SQL server 2005, to a default
I need to convert HTML documents into valid XML, preferably XHTML. What's the best
I need to convert a (possibly) null terminated array of ascii bytes to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.