I keep getting the same result with sample()

Hi guys, I'm just starting and I need help with something. It's a pretty basic thing, but I don't know how to solve this.

My issue is that I keep getting the same results with the sample() function, despite the fact that I've set.seed(1), do you know how can provide same basic assistance?

This is just an example of the problem:

> population <- unlist(femaleControlsPopulation)
> control <- sample(population, 12)
> treatment <- sample (population,12)
> mean(control)
[1] 22.6925
> mean(control)
[1] 22.6925
> mean(treatment)
[1] 26.14583
> mean(treatment)
[1] 26.14583
> mean(treatment)-mean(control)
[1] 3.453333
> mean(treatment)-mean(control)
[1] 3.453333

or

> y<-mean(sample(1000,15,replace = TRUE))
> y
[1] 622.4
> y
[1] 622.4
> y
[1] 622.4

Sorry for asking such a basic question.
Andres

The behavior you are seeing is correct. The function sample() is only executing when it’s called. Once you create treatment and/or y the function sample doesn’t get executed again no matter how many times you display the object contents or run mean on it.

If you want to execute it more than once you have to call the function itself again.

2 Likes

Many thanks @MattSpiegel

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.