4

I have a factor in R, that consists of the levels a, b and c. The data consists of 2 a's, one b and no c. I want to get an output like this (frequency of the elements according to the 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 with knitr/kable in a nice html table:

library(knitr)
kable(tbl)

But here comes the error:

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

I assume that there is an issue with the dimnames of the table:

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

Is there any option to "repair" the dimnames for kable? I just want to print this "simple" table - maybe I am stuck with something easy?

The usage of "table" with factors is described here: http://www.stat.berkeley.edu/~s133/factors.html

I read a lot about the 'pander'-package in the recent days. If I print the table with pander, it works. Why? Should I just switch to pander?

pander(tbl)
BumbleBee
  • 986
  • 9
  • 19
  • 1
    I am the maintainer of `pander`, so my view is somewhat skewed :) But `pander` offers a lot more options to [convert your R tables to markdown](http://rapporter.github.io/pander/#markdown-tables), not to mention the [support for a bunch of S3 classes](http://rapporter.github.io/pander/#generic-pander-method). – daroczig Jun 23 '15 at 16:47
  • I'll try that later on, it seems great. But I rather want to have the option to use different packages. I assume pander can do all that kable can do, but not the other way round? – BumbleBee Jun 24 '15 at 12:02
  • I am not sure about what's the advantage of trying to solve a problem with one package, which is already resolved by another one, but I am sure you have your motives. About `pander` vs `kable`: the purpose of these functions are a bit different. `kable` is to render basic markdown/LaTeX/HTML tables, while `xtable` can render complex `LaTeX` tables, and `pander` provides ~full support for markdown tables. – daroczig Jun 24 '15 at 16:22
  • Thank you for your extensive explanation here. I don't want to lead the discussion into a wrong direction, but does that mean, that one cannot use pander, when creating latex/pdf output? My idea was to have the document as flexible as possible - whether I want to create a pdf or html should not matter at first. I need the fine controls later on, when I create a paper or website. But nonetheless, I should dive deeper into those packages! – BumbleBee Jun 25 '15 at 15:26
  • Well, if you need flexibility, I'd stick with `pander`: it provides access to most markdown support (what `kable` might miss), which can be converted to either HTML, PDF, docx or similar. – daroczig Jun 25 '15 at 18:09

1 Answers1

6

You could try

> kable(t(as.matrix(tbl)))
#
#|  a|  b|  c|
#|--:|--:|--:|
#|  2|  1|  0|
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • 2
    The `table` format is not always very practical. It is therefore often convenient to convert a table into a matrix or into a data frame if the output is to be processed further. – RHertel Jun 23 '15 at 16:35
  • Thank you, this does the trick! I tried a data.frame, but it didn't work, so this is the best approach, probably. – BumbleBee Jun 24 '15 at 12:01