Raja Raja - 2 months ago 13
R Question

R Programming: sample() function return repeated entities even replace=TRUE

I am using sample() function to obtain a random sample from a dice object.

> die
[1] 1 2 3 4 5 6
> sample(x=die , size=1 , replace=TRUE)
[1] 1
> sample(x=die , size=1 , replace=TRUE)
[1] 1
> sample(x=die , size=1 , replace=TRUE)
[1] 5
> sample(x=die , size=1 , replace=TRUE)
[1] 5
> sample(x=die , size=1 , replace=TRUE)
[1] 1
> sample(x=die , size=1 , replace=TRUE)
[1] 2
> sample(x=die , size=1 , replace=TRUE)
[1] 4


If you observe above output 1 repeated 3 times and 5 repeated 2 times. But from above output even replace TRUE I could see frequent multiple duplications.

Ref: I am reading a book named Hands-On Programming with R.

Please let me know if more information required.

Thank you.

Answer

This is not a statistical, but a programming question and would be better suited for Stack Overflow.

You are using sample() incorrectly if your goal is to avoid getting the same number twice. To avoid repetitions, you need to create the sample with a single command (size more than 1 instead of multiple commands) and using replace=FALSE (not TRUE). For example:

> sample(x=die , size=4 , replace=FALSE)
[1] 1 6 4 3

Of course, this no longer corresponds to multiple fair dice rolls, as real dice can give you the same number in subsequent rolls.

See the help page ?sample for full information on how to use sample().

Comments