Getting table () to return zeros in R

This is my first question, and I'm pretty new to R , and I could not find how to do this.

So, I get some numbers that I want to put in the frequency table.

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 table(X) function, I get something similar to

 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 values โ€‹โ€‹of 9 and 10! I am trying to make values โ€‹โ€‹9, 10 with a zero number.

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

+6
source share
2 answers

I have not tested this, but I believe that you want

 table(factor(x, levels = 0:14)) 
+8
source

You can also get around this with the tabulate function by adding a constant to account for zeros.

 > tt <- tabulate(x+1) > names(tt) <- 0:14 > tt # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 17 27 9 8 13 5 5 5 5 0 0 2 2 1 1 
0
source

Source: https://habr.com/ru/post/903538/


All Articles