![](/img/trans.png)
[英]R:CrossTable- How to print frequencies for total row and column observations with xtable?
[英]How to specify row and column names in a crosstable when both are categorical
我有两个分类变量,我正在尝试为它们创建一个交叉表。 由于两个值都是 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|
或者
| | cat1| cat2|<br/>
|:---|--------:|---------:|<br/>
CAT1 | 83.41877| 92.217126|<br/>
|CAT2| 16.58123| 7.782874|<br/>
这取决于你最终想用它做什么,但如果目标是呈现降价,那么考虑使用 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 |
这两个包还提供了一些其他功能来制作整洁的表格,值得一看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.