yihui / printr

Some (magical) printing methods for knitr
https://yihui.org/printr/
118 stars 24 forks source link

support for multiway table? #12

Open xinkai-zhou opened 8 years ago

xinkai-zhou commented 8 years ago

printr gives beautiful output for table(). Is there any plan for supporting multi-way table generated by ftable()?

An RMD example:


---
title: "Untitled"
author: ""
date: ""
output: pdf_document

---

```{r, echo=FALSE}
library(knitr)
library(printr)
table(mtcars[c("cyl", "vs", "am", "gear")])
ftable(mtcars[c("cyl", "vs", "am", "gear")],
       row.vars = 1:3)
```  ```
yihui commented 8 years ago

No, I don't have such a plan yet. As long as you can convert the ftable output to a regular matrix or data frame, the rest will be easy. For the particular case you mentioned, I guess one way to go is to convert

          gear  3  4  5
cyl vs am              
4   0  0        0  0  0
       1        0  0  1
    1  0        1  2  0
       1        0  6  1
6   0  0        0  0  0
       1        0  2  1
    1  0        2  2  0
       1        0  0  0
8   0  0       12  0  0
       1        0  0  2
    1  0        0  0  0
       1        0  0  0

to something like this

cyl vs am/gear  3  4  5
4   0  0        0  0  0
       1        0  0  1
    1  0        1  2  0
       1        0  6  1
6   0  0        0  0  0
       1        0  2  1
    1  0        2  2  0
       1        0  0  0
8   0  0       12  0  0
       1        0  0  2
    1  0        0  0  0
       1        0  0  0

but this approach does not generalize to more complicated ftables like

       am    0        1      
       gear  3  4  5  3  4  5
cyl vs                       
4   0        0  0  0  0  0  1
    1        1  2  0  0  6  1
6   0        0  0  0  0  2  1
    1        2  2  0  0  0  0
8   0       12  0  0  0  0  2
    1        0  0  0  0  0  0

Personally I don't have much interest beyond simple rectangular tables. If you want to work on it, all I can say is "pull requests are welcome" :)

friendly commented 8 years ago

It is possible that vcd::structable() might give a data structure that is easier to work with