I have made a contingency table in R, shown below:
count <- table (dframe1$Contact, dframe1$Brokenbones) # create the table
count
Using gridExtra
, I think it is a good option to create smart table:
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)
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)
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:
> 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
> 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|
> 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 |
+--------------------+--------------------+------------------+-------+---------------+------+
> library(pander)
> pandoc.table(table(state.division, state.region), style = "grid", split.tables = Inf)
+--------------------------+-------------+---------+-----------------+--------+
| | 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 |
+--------------------------+-------------+---------+-----------------+--------+