[英]Element-wise concatenation of two matrices/data frames
Say I have a df:假设我有一个 df:
df=data.frame(
A=rep(c('a1','a1','a2','a2'),10),
B=rep(c('b1','b2','b2','b2'),10)
)
and I want a table like this:我想要一张这样的桌子:
What I can do is using table
, addMargins
and prop.table
to get two separate matrices, but I don't know if there has any efficient way to concatenate 2 matrices into the format I provide.我能做的是使用table
、 addMargins
和prop.table
来获得两个单独的矩阵,但我不知道是否有任何有效的方法将 2 个矩阵连接成我提供的格式。
>df%>%table()%>%addmargins()
B
A b1 b2 Sum
a1 10 10 20
a2 0 20 20
Sum 10 30 40
>df%>%table()%>%prop.table(margin=2)
B
A b1 b2
a1 1.00000 0.33333
a2 0.00000 0.66667
We can use sprintf
as shown:我们可以使用sprintf
如下所示:
tab.sum <- table(df)
tab.final <- addmargins(tab.sum)
tab.prop <- prop.table(tab.sum, margin=2)
tab.final[1:2, 1:2] <- sprintf("%d (%.2f%%)", tab.sum, tab.prop)
tab.final
giving:给予:
B
A b1 b2 Sum
a1 10 (1.00%) 10 (0.33%) 20
a2 0 (0.00%) 20 (0.67%) 20
Sum 10 30 40
however, you might want to try these packages:但是,您可能想尝试这些软件包:
1) Try CrossTable in gmodels. 1)在 gmodels 中尝试 CrossTable。 It has many arguments that can be used to customize the output.它有许多可用于自定义输出的参数。
library(gmodels)
with(df, CrossTable(A, B))
giving:给予:
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 40
| B
A | b1 | b2 | Row Total |
-------------|-----------|-----------|-----------|
a1 | 10 | 10 | 20 |
| 5.000 | 1.667 | |
| 0.500 | 0.500 | 0.500 |
| 1.000 | 0.333 | |
| 0.250 | 0.250 | |
-------------|-----------|-----------|-----------|
a2 | 0 | 20 | 20 |
| 5.000 | 1.667 | |
| 0.000 | 1.000 | 0.500 |
| 0.000 | 0.667 | |
| 0.000 | 0.500 | |
-------------|-----------|-----------|-----------|
Column Total | 10 | 30 | 40 |
| 0.250 | 0.750 | |
-------------|-----------|-----------|-----------|
2) Also CrossTable in descr. 2)也是描述中的 CrossTable。
library(descr)
with(df, CrossTable(A, B))
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
==============================
B
A b1 b2 Total
------------------------------
a1 10 10 20
5.000 1.667
0.5 0.5 0.5
1.000 0.333
0.25 0.25
------------------------------
a2 0 20 20
5.000 1.667
0.0 1.0 0.5
0.000 0.667
0.00 0.50
------------------------------
Total 10 30 40
0.25 0.75
==============================
3) The tables package has a formula interface. 3) tables包有公式接口。 It also supports latex output.它还支持乳胶输出。
tabular((A + 1)
*((n=1) + Percent()
+ (RowPct=Percent("row"))
+ (ColPct=Percent("col")))
~ (B + 1)
*Format(digits=1), data=df )
giving:给予:
B
A b1 b2 All
a1 n 10 10 20
Percent 25 25 50
RowPct 50 50 100
ColPct 100 33 50
a2 n 0 20 20
Percent 0 50 50
RowPct 0 100 100
ColPct 0 67 50
All n 10 30 40
Percent 25 75 100
RowPct 25 75 100
ColPct 100 100 100
4) janitor 4) 看门人
The janitor package supports a pipeline specification and supports piping its output to kable.看门人包支持管道规范并支持将其输出管道传输到 kable。
library(janitor)
df %>%
tabyl(A, B) %>%
adorn_totals(c("row", "col")) %>%
adorn_percentages("all") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns %>%
adorn_title
giving:给予:
B
A b1 b2 Total
a1 25.0% (10) 25.0% (10) 50.0% (20)
a2 0.0% (0) 50.0% (20) 50.0% (20)
Total 25.0% (10) 75.0% (30) 100.0% (40)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.