Randomly generate 100 pets from the choices dog
, cat
, hamster
and goldfish
with equal probability of each being chosen. Display the first six values in pets
and store these in the object first
. Count the number of each pet and store these in the object counts
(Hint: you may need to convert pets
to a factor first).
(from: Cotton, 2013, Learning R, O’Reilly)
Because you will be generating a random sample, we set a seed for R’s random number generator, so your results will be the same as Dodona’s.