I need a table showing even the zero counts. x<-c(1,1,1,2,2,2,5,5,5) table(x) x 1 2 5 3 3 3 How can I get this: x 1 2 3 4 5 3 3 0 0 3 Thanks, [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Oct-31 15:44 UTC
[R] How to show classes with 0 count in table()?
It's not a totally general solution but something like table( factor(x, levels = min(x):max(x))) will do it in this case. The key is to make understand that R converts your data to a factor if necessary and then checks each level of that factor. Here we are taking care of the factor conversion explicitly. Michael On Mon, Oct 31, 2011 at 8:35 AM, Leonardo Bergamini <llbergamini at gmail.com> wrote:> I need a table showing even the zero counts. > > x<-c(1,1,1,2,2,2,5,5,5) > table(x) > x > 1 2 5 > 3 3 3 > > > How can I get this: > > x > 1 2 3 4 5 > 3 3 0 0 3 > > Thanks, > > ? ? ? ?[[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. >
?tabulate> x<-c(1,1,1,2,2,2,5,5,5) > tabulate(x)[1] 3 3 0 0 3>On Mon, Oct 31, 2011 at 8:35 AM, Leonardo Bergamini <llbergamini at gmail.com> wrote:> I need a table showing even the zero counts. > > x<-c(1,1,1,2,2,2,5,5,5) > table(x) > x > 1 2 5 > 3 3 3 > > > How can I get this: > > x > 1 2 3 4 5 > 3 3 0 0 3 > > Thanks, > > ? ? ? ?[[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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
> x<-matrix(c(1,2,3,4,5,3,3,0,0,3),nrow=2,ncol=5,byrow=TRUE) > x[,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 3 3 0 0 3>-----Original Message----- From: Leonardo Bergamini Sent: Monday, October 31, 2011 1:35 PM To: r-help at r-project.org Subject: [R] How to show classes with 0 count in table()? I need a table showing even the zero counts. x<-c(1,1,1,2,2,2,5,5,5) table(x) x 1 2 5 3 3 3 How can I get this: x 1 2 3 4 5 3 3 0 0 3 Thanks, [[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.
Thanks you all. 2011/10/31 Leonardo Bergamini <llbergamini@gmail.com>> I need a table showing even the zero counts. > > x<-c(1,1,1,2,2,2,5,5,5) > table(x) > x > 1 2 5 > 3 3 3 > > > How can I get this: > > x > 1 2 3 4 5 > 3 3 0 0 3 > > Thanks, > > > >[[alternative HTML version deleted]]