![](/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.