Print the coefficient frequency table with kable in rmarkdown

I have a factor in R consisting of levels a, b and c. The data consists of 2 a, one b and no c. I want to get this output (frequency of elements according to levels):

fac <- factor(c("a", "b", "a"), levels=c("a", "b", "c"))
tbl <- table(fac)
tbl

## fac
## a b c 
## 2 1 0

This should be printed using knitr / kable in a good html table:

library(knitr)
kable(tbl)

But here an error occurs:

"Error in dn[[2L]] : subscript out of bounds".

I assume there is a problem with the table dimnames:

attributes(tbl)
## $dim
## [1] 3
## 
## $dimnames
## $dimnames$fac
## [1] "a" "b" "c"
## 
## 
## $class
## [1] "table"

Is there an option to "restore" dimnames for kable? I just want to print this “simple” table - maybe I'm stuck with something easy?

The use of a “table” with factors is described here: http://www.stat.berkeley.edu/~s133/factors.html

"" . pander, . ? pander?

pander(tbl)
+4
1

> kable(t(as.matrix(tbl)))
#
#|  a|  b|  c|
#|--:|--:|--:|
#|  2|  1|  0|
+5

All Articles