rnorouzian
rnorouzian

Reputation: 7517

Creating shuffled numbers in R

As a result of seeing THIS EXAMPLE, I was wondering how I could create one set of 15 shuffled orderings of 1 through 4 in R?

On THIS Website, you can get 1 Set of 15 shuffled Numbers Ranging: From 1 to 4

As an example, on my run I got:

Set #1: 3, 2, 2, 1, 1, 1, 3, 2, 2, 3, 2, 1, 3, 4, 1

Is there a way I can replicate the above in R?

Upvotes: 0

Views: 68

Answers (1)

Ric S
Ric S

Reputation: 9247

If I understood correctly your question, at first it comes to mind a solution like the following one: very basic, but it does its job.

size <- 40
vec <- sample(1:4, size = size, replace = TRUE)
while(length(unique(vec)) < 4){
    vec <- sample(1:4, size = size, replace = TRUE)
}
vec 

The while cycle will not go on for long as it's very unlikely that a digit does not appear in the random vector vec if you sample 40 times.

Of course you can change the size of your vector, the code will still work, except you want vec to be < 4; in that case, the loop will go on indefinitely.

Upvotes: 2

Related Questions