3

Just starting out with R and trying to figure out what works for my needs when it comes to creating "summary tables." I am used to Custom Tables in SPSS, and the CrossTable function in the package gmodels gets me almost where I need to be; not to mention it is easy to navigate for someone just starting out in R.

That said, it seems like the Hmisc table is very good at creating various summaries and exporting to LaTex (ultimately what I need to do).

My questions are:1)can you create the table below easily in the Hmsic page? 2) if so, can I interact variables (2 in the the column)? and finally 3) can I access p-values of significance tests (chi square).

Thanks in advance,

Brock

   Cell Contents
|-------------------------|
|                   Count |
|             Row Percent |
|          Column Percent |
|-------------------------|

Total Observations in Table:  524 

             | asq[, 23] 
    asq[, 4] |        1  |        2  |        3  |        4  |        5  | Row Total | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|
           0 |       76  |       54  |       93  |       46  |       54  |      323  | 
             |   23.529% |   16.718% |   28.793% |   14.241% |   16.718% |   61.641% | 
             |   54.286% |   56.250% |   63.265% |   63.889% |   78.261% |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|
           1 |       64  |       42  |       54  |       26  |       15  |      201  | 
             |   31.841% |   20.896% |   26.866% |   12.935% |    7.463% |   38.359% | 
             |   45.714% |   43.750% |   36.735% |   36.111% |   21.739% |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|
Column Total |      140  |       96  |      147  |       72  |       69  |      524  | 
             |   26.718% |   18.321% |   28.053% |   13.740% |   13.168% |           | 
-------------|-----------|-----------|-----------|-----------|-----------|-----------|
Btibert3
  • 38,798
  • 44
  • 129
  • 168

2 Answers2

4

The gmodels package has a function called CrossTable, which is very nice for those used to SPSS and SAS output. Try this example:

library(gmodels)  # run install.packages("gmodels") if you haven't installed the package yet
x <- sample(c("up", "down"), 100, replace = TRUE)
y <- sample(c("left", "right"), 100, replace = TRUE)
CrossTable(x, y, format = "SPSS")

This should provide you with an output just like the one you displayed on your question, very SPSS-y. :)

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
  • @Stedy, I think you're right. I'm not sure what I was thinking back then. Thanks for the tip, I'll edit the answer accordingly. – Waldir Leoncio Sep 06 '14 at 13:05
3

If you are coming from SPSS, you may be interested in the package Deducer ( http://www.deducer.org ). It has a contingency table function:

> library(Deducer)
> data(tips)
> tables<-contingency.tables(
+ row.vars=d(smoker),
+ col.vars=d(day),data=tips)
> tables<-add.chi.squared(tables)
> print(tables,prop.r=T,prop.c=T,prop.t=F)
================================================================================================================

               ==================================================================================               
                                   ========== Table: smoker by day ==========                                   
                       | day 
                smoker |      Fri  |      Sat  |      Sun  |     Thur  | Row Total | 
-----------------------|-----------|-----------|-----------|-----------|-----------|
          No  Count    |        4  |       45  |       57  |       45  |      151  | 
              Row %    |    2.649% |   29.801% |   37.748% |   29.801% |   61.885% | 
              Column % |   21.053% |   51.724% |   75.000% |   72.581% |           | 
-----------------------|-----------|-----------|-----------|-----------|-----------|
         Yes  Count    |       15  |       42  |       19  |       17  |       93  | 
              Row %    |   16.129% |   45.161% |   20.430% |   18.280% |   38.115% | 
              Column % |   78.947% |   48.276% |   25.000% |   27.419% |           | 
-----------------------|-----------|-----------|-----------|-----------|-----------|
          Column Total |       19  |       87  |       76  |       62  |      244  | 
              Column % |    7.787% |   35.656% |   31.148% |   25.410% |           | 



            Large Sample                                                       
       Test Statistic    DF p-value | Effect Size est.  Lower (%)   Upper (%)  
Chi Squared 25.787       3  <0.001  | Cramer's V  0.325 0.183 (2.5) 0.44 (97.5)
-----------





================================================================================================================

You can get the counts and test to latex or html using the xtable package:

> library(xtable)
> xtable(drop(extract.counts(tables)[[1]]))
> test <- contin.tests.to.table((tables[[1]]$tests))
> xtable(test)
Ian Fellows
  • 17,228
  • 10
  • 49
  • 63
  • This is great - I didn't know about this package. I really am interested in mimicing Custom Tables which lets me interact variables (both column and row) as well as simply create tables with multiple variables as well and figure out the best ways to create the reports - looks like LaTeX – Btibert3 Jul 28 '10 at 18:54