lundi 30 mai 2016

Generate random number of fixed length keeping with leading zeroes in R

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.




Aucun commentaire:

Enregistrer un commentaire