[R] creating empty cells with table()

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 19 15:52:59 CEST 2006

One can use the fact that converting a zoo object to a
ts object fills in the holes with NAs.

First create some test data, x, and create the table, tab.
Then create a zoo object and convert that to a ts object.
Now barplot the ts values using the times as names.

x <- rpois(20, 4) # test data

tab <- table(x)
tt <- as.ts(zoo(as.vector(tab), as.numeric(names(tab))))
barplot(coredata(tt), names = time(tt))

On 4/19/06, Owen, Jason <wowen at richmond.edu> wrote:
> Hello,
> Suppose I simulate 20 observations from the Poisson distribution
> with lambda = 4.  I can summarize these values using table() and
> then feed that to barplot() for a graph.
> Problem: if there are empty categories (e.g. 0) or empty categories
> within the range of the data (e.g. observations for 6, 7, 9), barplot()
> does not include the empty cells in the x-axis of the plot.  Is there
> any way to specify table() to have specific categories (in the above
> example, probably 0:12) so that zeroes are included?
> Thanks,
> Jason
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list