如何在R中构建的列联表中创建边框?

时间:2013-12-08 17:27:30

标签: r

我在R中制作了一个列联表,如下所示:

count <- table (dframe1$Contact, dframe1$Brokenbones)          # create the table

 count                                                          # check the table

      0   1
  0 235  61
  1  54  32

dimnames(count) = list(

c("No contact sport", "Contact sport"),   # adds row names
c("No broken ankle/leg ", "Broken ankle/leg"))# adds column names
count


                 No broken ankle/leg  Broken ankle/leg
No contact sport                  235               61
Contact sport                      54               32

我想为它添加边框,以定义'单元格'。这可能在R?

3 个答案:

答案 0 :(得分:2)

也许你可以使用“pander”或“knitr”中的东西。 (我假设您只是指控制表在控制台中的显示方式 - 否则,如果您尝试创建可用于发布的表,还有许多其他更好的建议。)

示例:

R

中的基本表
> table(state.division, state.region)
                    state.region
state.division       Northeast South North Central West
  New England                6     0             0    0
  Middle Atlantic            3     0             0    0
  South Atlantic             0     8             0    0
  East South Central         0     4             0    0
  West South Central         0     4             0    0
  East North Central         0     0             5    0
  West North Central         0     0             7    0
  Mountain                   0     0             0    8
  Pacific                    0     0             0    5

通过knitr的Markdown表:

> library(knitr)
> kable(table(state.division, state.region))
|id                  |  Northeast|  South|  North Central|  West|
|:-------------------|----------:|------:|--------------:|-----:|
|New England         |          6|      0|              0|     0|
|Middle Atlantic     |          3|      0|              0|     0|
|South Atlantic      |          0|      8|              0|     0|
|East South Central  |          0|      4|              0|     0|
|West South Central  |          0|      4|              0|     0|
|East North Central  |          0|      0|              5|     0|
|West North Central  |          0|      0|              7|     0|
|Mountain            |          0|      0|              0|     8|
|Pacific             |          0|      0|              0|     5|

使用ascii包:

> library(ascii)
> print(ascii(table(state.division, state.region)), type = "rest")

+--------------------+--------------------+------------------+-------+---------------+------+
|                                         | **state.region**                                |
+                                         +------------------+-------+---------------+------+
|                                         | Northeast        | South | North Central | West |
+====================+====================+==================+=======+===============+======+
| **state.division** | New England        | 6.00             | 0.00  | 0.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | Middle Atlantic    | 3.00             | 0.00  | 0.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | South Atlantic     | 0.00             | 8.00  | 0.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | East South Central | 0.00             | 4.00  | 0.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | West South Central | 0.00             | 4.00  | 0.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | East North Central | 0.00             | 0.00  | 5.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | West North Central | 0.00             | 0.00  | 7.00          | 0.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | Mountain           | 0.00             | 0.00  | 0.00          | 8.00 |
+                    +--------------------+------------------+-------+---------------+------+
|                    | Pacific            | 0.00             | 0.00  | 0.00          | 5.00 |
+--------------------+--------------------+------------------+-------+---------------+------+

使用pander package

> library(pander)
> pandoc.table(table(state.division, state.region), style = "grid", split.tables = Inf)


+--------------------------+-------------+---------+-----------------+--------+
|          &nbsp;          |  Northeast  |  South  |  North Central  |  West  |
+==========================+=============+=========+=================+========+
|     **New England**      |      6      |    0    |        0        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|   **Middle Atlantic**    |      3      |    0    |        0        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|    **South Atlantic**    |      0      |    8    |        0        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|  **East South Central**  |      0      |    4    |        0        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|  **West South Central**  |      0      |    4    |        0        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|  **East North Central**  |      0      |    0    |        5        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|  **West North Central**  |      0      |    0    |        7        |   0    |
+--------------------------+-------------+---------+-----------------+--------+
|       **Mountain**       |      0      |    0    |        0        |   8    |
+--------------------------+-------------+---------+-----------------+--------+
|       **Pacific**        |      0      |    0    |        0        |   5    |
+--------------------------+-------------+---------+-----------------+--------+

答案 1 :(得分:1)

使用gridExtra,我认为创建智能表是个不错的选择:

enter image description here

mm <- matrix(c(235,61,54,32),ncol=2,byrow=TRUE,
        dimnames = list(c("No contact sport", "Contact sport"), 
        c("No broken ankle/leg ", "Broken ankle/leg")))
library(gridExtra)
grid.table(mm)


library(gridExtra)
grid.table(mm)

答案 2 :(得分:-1)

这是另一种可能性,与观星者包。它的主要目标是生成外观漂亮的lm()表,但您可以做更多。

require(stargazer)
star <- stargazer(mytable, type = "text", 
                  title = "Stargazer with Summary = FALSE", 
                  summary = FALSE)