The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

m.dr (70) [Avatar] Offline
#1
Just getting started with R and R In Action.

Question on sampling the data - the sampling below with replace either TRUE or FALSE always return me the same data:

sample(nrow(movies), size=10, replace=TRUE)

sample(nrow(movies), size=10, replace=FALSE)

They return different sets of indexes but the same sets always.

I first noticed it when I was sampling a small set but then I looked into with slightly larger numbers and noticed its returning the same indexes.

I think it should not matter if I am just executing the command above by itself just to get a sample whether I use replace as TRUE or FALSE.

But I wrote a small script that I executed repeatedly to see about the indexes I got and I keep getting the same indexes.

Am I understanding the sample function totally wrong?

Thanks.
robert.kabacoff (170) [Avatar] Offline
#2
Re: Sampling the data
Hi,

The sample function takes a sample of a set of numbers. If replace=TRUE, the numbers are put back before each draw.

So for example,

> x <- 20
> sample(x, size=10, replace=TRUE)
[1] 9 7 14 10 4 19 18 4 16 18
> sample(x, size=10, replace=FALSE)
[1] 1 8 11 15 13 20 3 16 14 12

Here we are sampling 10 numbers from 1 through 20.
With replace=TRUE you see that 18 was selected twice.

With replace=FALSE you see that no number was selected more than once.

Hope this helps.
m.dr (70) [Avatar] Offline
#3
Re: Sampling the data
This worked out as well.

Btw I did not have to change any code, but when I was calling it in a script it was returning me the same numbers. After I restarted R it magically started working.

But thanks for this as well.