0

I'm taking a sample of size 1000 of different (pet) animals. My code is

pet <- sample(c("dog","cat","hamster","goldfish"), 1000, replace = TRUE)

I want to see how many times "dog" was selected, "cat" was selected, etc. I've tried summary(pet) but wasn't much help / just told me it is length 1000 and characters.

ABC
  • 11
  • 1
  • 1
    Your question is completely unclear. You should try at least to inform in which language you are coding. If it's random distribution the law of large numbers says that, having $n_c$ class, each class has probability $P(c_i) = \frac{1}{n_c}$ – giuseppe Nov 01 '14 at 21:48
  • oh,sorry. I'm using R. So when I run the code it gives me 1000 different animals like so (this is just the first 20 or so) [1] "dog" "hamster" "hamster" "dog" "goldfish" "goldfish" "dog" [8] "goldfish" "dog" "goldfish" "dog" "dog" "dog" "dog" [15] "cat" "hamster" "goldfish" "goldfish" "goldfish" "hamster" "hamster" – ABC Nov 01 '14 at 23:03
  • What I want to do is count the number of times each of the four animals/strings appear in that specific sample, which won't be 250 exactly. – ABC Nov 01 '14 at 23:09
  • This isn't a statistical question; it is an R coding question. We will migrate this to SO for you. – gung - Reinstate Monica Nov 02 '14 at 03:05

2 Answers2

3

Try in R:

> table(pet)
pet
     cat      dog goldfish  hamster 
     241      284      225      250 
rnso
  • 23,686
  • 25
  • 112
  • 234
1

The way you are creating this variable creates a vector of character data. Consider:

> pet <- sample(c("dog","cat","hamster","goldfish"), 1000, replace = TRUE)
> str(pet)
 chr [1:1000] "cat" "dog" "dog" "goldfish" "dog" "dog" ...

You need a factor vector with specific levels. Try:

> summary(as.factor(pet))
     cat      dog goldfish  hamster 
     252      244      252      252 
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79