rmuc8
rmuc8

Reputation: 2989

Crosstable similar to Stata in R

I am working with a UCLA sample data set

> read <- read.table("http://www.ats.ucla.edu/stat/mult_pkg/faq/general/sample.csv",
          header=TRUE, sep=",", quote="\"")
> head(read)
  female read write math hon femalexmath
1      0   57    52   41   0           0
2      1   68    59   53   0          53
3      0   44    33   54   0           0
4      0   63    44   47   0           0

and I want to crosstab the variables hon with female

The desired result looks like this stata output:

           |        female
       hon |      male     female |     Total
-----------+----------------------+----------
         0 |        74         77 |       151 
         1 |        17         32 |        49 
-----------+----------------------+----------
     Total |        91        109 |       200 

Using R, I tried to use xtabs

> xtabs(female~hon, data = read)
hon
 0  1 
77 32 

and reshape2

> library(reshape2)
> melt <- melt(read, id="female")
> dcast(melt, variable ~ female, sum, subset = .(variable == "hon"))
hon
 0  1 
77 32 

and table

> table(read$hon, read$female)
     0  1
  0 74 77
  1 17 32

but this is only a part of the desired result

I'd like to include the non-female (=male) values and calculate the total numbers, and assign names appropriately.

Am I missing an easy function for that in R?

I have seen this post Mimic tabulate command from Stata in R, but since the code in this question did not contain the library gmodels for CrossTable, I couldn't apply it. The output also looks different.

Upvotes: 3

Views: 1822

Answers (1)

Sam Firke
Sam Firke

Reputation: 23014

library(gmodels)
read$gender <- ifelse(read$female==1, "Female", "Male")
with(read, CrossTable(hon, gender, prop.c=FALSE, prop.r = FALSE, prop.t = FALSE, prop.chisq = FALSE))

Returns:

   Cell Contents
|-------------------------|
|                       N |
|-------------------------|


Total Observations in Table:  200 


             | gender 
         hon |    Female |      Male | Row Total | 
-------------|-----------|-----------|-----------|
           0 |        77 |        74 |       151 | 
-------------|-----------|-----------|-----------|
           1 |        32 |        17 |        49 | 
-------------|-----------|-----------|-----------|
Column Total |       109 |        91 |       200 | 
-------------|-----------|-----------|-----------|

Specifying all of those arguments is tedious, so if you find yourself using the CrossTable() function often you could write a wrapper function:

tab <- function(x, y) {
  argx <- deparse(substitute(x))
  argy <- deparse(substitute(y))
  return(CrossTable(x, y, prop.c=FALSE, prop.r = FALSE, prop.t = FALSE, prop.chisq = FALSE, dnn = c(argx, argy)))
}

Call it like this:

with(read, tab(hon, gender))

Upvotes: 2

Related Questions