代码之家  ›  专栏  ›  技术社区  ›  maloneypatr

R输出看起来像文本表[duplicate]

  •  0
  • maloneypatr  · 技术社区  · 7 年前

    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中可能吗?

    0 回复  |  直到 12 年前
        1
  •  1
  •   agstudy    12 年前

    使用 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
  •  2
  •   daroczig    12 年前

    例子:

    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的降价表:

    > 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    |
    +--------------------------+-------------+---------+-----------------+--------+
    
        3
  •  -1
  •   A5C1D2H2I1M1N2O1R2T1    12 年前

    这是另一种可能性,与星探包。它的主要目标是产生好看的 lm() 桌子,但你可以做得更多。

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