[R] Showing zero frequencies with xtabs

Petr PIKAL petr.pikal at precheza.cz
Tue Aug 30 10:19:54 CEST 2011


Hi
> 
> Hi,
> 
> Does anyone know how to show zero frequencies variable levels with the 
> xtabs command? They show with the table(x,y) command but I need to apply 

> weight to frequency tables and I also need to cbind several tables 
> together, which implies that they all need to show the same number of 
rows. 

Do you use factors?

x<-sample(letters[1:5], 20, replace=T)
xtabs(~x)
x
a b c d e 
2 1 5 4 8 

x.f<-factor(x)
x[x=="b"]<-NA
x.f[x.f=="b"]<-NA

> xtabs(~x)
x
a c d e 
2 5 4 8 

> xtabs(~x.f)
x.f
a b c d e 
2 0 5 4 8 

With factors you can specify also non existent levels so they shall appear 
as zeroes in table or xtabs.

Regards
Petr

> 
> Alternatively, do you know how to column bind tables with different 
number
> of rows? I cannot use merge as it requires daata.frame and that modifies 

> the look of the banner table I am trying to create...
> 
> Thanks,
> Luca
> 
> 
> Mr. Luca Meyer
> www.lucameyer.com
> R version 2.13.1 (2011-07-08)
> Mac OS X 10.6.8
> 
> 
> 
> 
> 
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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