我有两个类别变量,我正在尝试为它们创建一个交叉表。由于这两个值都是yes和no,因此我想指定行和列的名称以便于理解。
T4 <- table(bank$Term_deposit, bank$housing_loan)
%>% prop.table(margin = 2) *100
T4
no yes
no 83.418772 92.217126 <br/>
yes 16.581228 7.782874
kable(T4, caption = "%agewise comparison for marital status")
| | no| yes|<br/>
|:---|--------:|---------:|<br/>
|no | 83.41877| 92.217126|<br/>
|yes | 16.58123| 7.782874|<br/>
预期输出:
| | no| yes|<br/>
|:---|--------:|---------:|<br/>
CAT1 | 83.41877| 92.217126|<br/>
|CAT2| 16.58123| 7.782874|
OR
| | cat1| cat2|<br/>
|:---|--------:|---------:|<br/>
CAT1 | 83.41877| 92.217126|<br/>
|CAT2| 16.58123| 7.782874|<br/>
答案 0 :(得分:2)
这最终取决于您要执行的操作,但是如果目标是渲染降价,请考虑使用pandoc.table,因为它提供的功能类似于knitr :: kable:
library(pandoc)
colnames(T4) <- c("CAT1", "CAT2")
rownames(T4) <- c("no", "yes")
pandoc.table(T4)
输出:
-------------------------
CAT1 CAT2
-------- -------- -------
**y** 13473 77311
**n** 226221 0
-------------------------
或者:
colnames(T4) <- c("deposit: no", "deposit: yes")
rownames(T4) <- c("loan: no", "loan: yes")
pandoc.table(T4)
输出:
--------------------------------------------
deposit: no deposit: yes
--------------- ------------- --------------
**loan: no** 13473 77311
**loan: yes** 226221 0
--------------------------------------------
另一种可能性是使用expss包:
library(expss)
df <- apply_labels(bank,
Term_deposit= "Term deposit",
housing_loan= "Housing loan")
cro_cpct(bank$Term_deposit, bank$housing_loan)
输出:
| | | Housing loan | |
| | | 0 | 1 |
| ------------ | ------------ | ------------ | --- |
| Term deposit | 0 | 39.4 | 100 |
| | 1 | 60.6 | |
| | #Total cases | 66.0 | 34 |
这两个软件包还提供了其他一些功能来制作整洁的表格,值得一看。