[R] Creating a frequency table for binomial varaible

William Dunlap wdunlap at tibco.com
Wed Nov 21 17:07:31 CET 2012


> I know that the simple  R function table() will do this, but I am afraid
> that some times I may get zero frequency for some particular values 

Make a factor out of your data, specifying all the levels you want counts
for, and pass that factor to table().  E.g.,

   >   x <- rep(0:6, c(5,2,0,3,0,4,0))
   >   table(x)
   x
   0 1 3 5 
   5 2 3 4
   >   table(factor(x, levels=0:10))
   
    0  1  2  3  4  5  6  7  8  9 10 
    5  2  0  3  0  4  0  0  0  0  0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of arun4
> Sent: Wednesday, November 21, 2012 5:20 AM
> To: r-help at r-project.org
> Subject: [R] Creating a frequency table for binomial varaible
> 
> Hello,
> I have simulated 30  observations from a binomial(5,0.1) distribution.
> Now I need to make frequency table( that means I need to tally how many 0's
> , 1's  2's....... 5's)
> I know that the simple  R function table() will do this, but I am afraid
> that some times I may get zero frequency for some particular values (for
> example in the above there are 5-0's 10-1's , 14-2's, 10-3's , 11-4's but no
> any 5's )
> 
> So I want to make by frequecy table ( as a date frame) as
> value   freq
> 0           5
> 1          10
> 2          14
> 3          10
> 4           11
> 5           0
> 
> 
> How can I create such a table?
> Forgive me if this is a very basic question. I am new to R.
> 
> Thank you very much.
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Creating-a-frequency-
> table-for-binomial-varaible-tp4650286.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list