How do I make borders in a contingency table built in R?

前端 未结 3 1123
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 10:55

I have made a contingency table in R, shown below:

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

 count                           


        
相关标签:
3条回答
  • 2021-01-26 11:47

    Using gridExtra, I think it is a good option to create smart table:

    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)
    
    0 讨论(0)
  • 2021-01-26 11:49

    Here is yet another possibility, with the stargazer package. It's primary goal is to produce good-looking lm() tables, but you can do more.

    require(stargazer)
    star <- stargazer(mytable, type = "text", 
                      title = "Stargazer with Summary = FALSE", 
                      summary = FALSE)
    
    0 讨论(0)
  • 2021-01-26 11:53

    Perhaps you can use something from "pander" or "knitr". (I'm assuming you're just referring to how the table is displayed in the console--otherwise, there are many other much better recommendations if you are trying to create publication-ready tables.)

    Example:

    Basic table in 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
    

    Markdown table via knitr:

    > 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|
    

    With the ascii package:

    > 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 |
    +--------------------+--------------------+------------------+-------+---------------+------+
    

    With the 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    |
    +--------------------------+-------------+---------+-----------------+--------+
    
    0 讨论(0)
提交回复
热议问题