Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting table() to return zeroes in R [duplicate]

Tags:

r

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.

like image 960
user1021000 Avatar asked Dec 11 '11 00:12

user1021000


1 Answers

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

table(factor(x, levels = 0:14))
like image 103
Allen Z. Avatar answered Sep 18 '22 00:09

Allen Z.