2017-07-25 1 views
1

De quelle manière puis-je construire une table de fréquence bidirectionnelle dans Rmarkdown? Quelque chose comme:Tableau des fréquences bidirectionnelles dans Rmarkdown

this

J'ai essayé d'utiliser à la fois la fonction Kable du paquet knitr et la fonction datable du paquet DT, mais aucun ne m'a donné le résultat souhaité.

Mise à jour: Code reproductible avec exemple.

a <- sample(x = c(1,2,3), size = 1000, replace = T) 
b <- sample(x = c('a', 'b', 'c'), size = 1000, replace = T) 

df <- data.frame(vertical_title = a, horitzontal_title = b) 

table(df) 

       horitzontal_title 
vertical_title a b c 
      1 118 98 106 
      2 106 95 121 
      3 128 114 114 

Je veux 'vertical_title' et 'horizontal_title' pour être visible pour ma table Rmarkdown.

+0

Votre question est difficile à répondre comme ça. S'il vous plaît voir [ici] (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) pour obtenir des conseils sur la façon de fournir un exemple reproductible. – Florian

+0

Bon point Florian, j'étais un peu pressé. J'ai ajouté un exemple reproductible. – Michael

Répondre

0

Vous pouvez construire le tableau comme suit:

--- 
title: Example 1 
output: pdf_document 
--- 

### This is a test 

```{r,results='asis',echo=FALSE} 

library(xtable) 
a <- sample(x = c(1,2,3), size = 1000, replace = T) 
b <- sample(x = c('a', 'b', 'c'), size = 1000, replace = T) 

df <- data.frame(vertical_title = a, horitzontal_title = b) 
df <- as.data.frame.matrix(table(df)) 


print(xtable(df, 
      align = "|l|rrr|", 
      caption = "Test"), 
     caption.placement = 'top', 
     comment = F, 
     scalebox=1, 
     include.rownames = T, 
     hline.after = c(-1,0,nrow(df)), 
     vline.after = c(1), 
     format.args=list(big.mark = ",", decimal.mark = ".")) 


``` 

Voir ici pour un moyen d'ajouter multicolumn et multirows pour vos titres:

R package xtable, how to create a latextable with multiple rows and columns from R

Hope this helps!