6

This is my first question, and I'm quite new to R and I haven't been able to find how to do it.

So I get some numbers that I want to put into table of frequencies.

12  0 12  2  1 14  1  1  0  3  1  2  8  0  1  3 11  1  1  8  8  8  0  4  4
 2  6  1  1  4  1  1  7  0  6  4  6  1  1  1  1  2  4  2  3  7  3  1  1  7
 0  0 11  8  1  5  0  5  6  0  0  0 13  1  1  5  2  7  2  1  7  3  4  4  2
 4  0  4  4  0  4  0  2  1  1  1  0  5  6  1  4  1  5  3  3  4  0  3  1  0

When I use the function table(X), I get something that looks like

X
 0  1  2  3  4  5  6  7  8 11 12 13 14 

17 27  9  8 13  5  5  5  5  2  2  1  1 

Which leaves out the values 9 and 10! I am trying to make the values 9, 10 appear with zero count.

I have tried using xtabs and tapply, but I'm just not sure how to do this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1021000
  • 237
  • 1
  • 3
  • 8

1 Answers1

9

I haven't tested this, but I believe you want

table(factor(x, levels = 0:14))
Allen Z.
  • 1,560
  • 1
  • 11
  • 19
  • 1
    to be a bit more general you could use levels = min(x):max(x), and add missing value handling etc. – mdsumner Dec 13 '11 at 00:25