mercredi 30 janvier 2019

R: producing vector with number of times an element appears in sample

I have a homework problem where I have a sample of 30 men, a random sampling of 10 of them:

men [1] 15 18 14 6 22 17 20 3 16 9

And From them, do 12 random samples and determine how many times each man appears.

The problem statement, verbatim, is "Perform 12 samples of 10 men from a population of size 30 and for each man, record the number samples in which he appears."

I have attempted a loop for the problem that would produce a vector of 10 elements, each one lined up with the appropriate index.

mtimes<-rep(0,12)
> repeat{
+ mtimes[menind]<-sum(sample(pop1,12,replace = TRUE) == men[menind])
+ menind = menind + 1
+ if (menind == 10){
+ break
+ }
+ }

This resulted in a vector:

mtimes [1] 0 0 1 0 0 0 0 0 0 0

It seems the 3rd man should not have appeared only once while no one else appeared in the samples.




Aucun commentaire:

Enregistrer un commentaire