0

I would like to accomplish something very similiar to this question but in R.

In essence, I want to generate a sequence of random numbers with a fixed length (say, 4), and if the numbers are less than 4 digits in length then they will have leading zeroes.

For example, a random sequence which could include 0000, 0001.... 9999. Such numbers could be suited to be used as ID numbers for example - but they must be integers, not strings.

I know that I can use sample(0:9999, n, replace = TRUE where n is the number of repetitions, however this creates the possibility of numbers that are less than 4 digits in length.

In addition, I'm aware of a method using sprintf() that adds two leading zeroes and converts to a string, but this won't suit my needs.

I am new to R.

Community
  • 1
  • 1
  • Can't you create two columns? One with an integer and one `as.character` with leading zeros and then use them separately for applications that require an ID or an integer. – nya May 30 '16 at 13:49
  • you need to tell us a little bit more about what you're trying to do. Numbers in R don't have formats -- they are only formatted when you're printing them out. – Ben Bolker May 30 '16 at 13:55
  • Thank you very much - I did not see that post – new_analyst May 30 '16 at 14:16

0 Answers0