3

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.

Community
  • 1
  • 1
rmuc8
  • 2,869
  • 7
  • 27
  • 36
  • 3
    `addmargins(table(read$hon, read$female))`? – David Arenburg Mar 31 '15 at 15:03
  • possible duplicate of [Mimic tabulate command from Stata in R](http://stackoverflow.com/questions/13043817/mimic-tabulate-command-from-stata-in-r) – Sam Firke Mar 31 '15 at 15:05
  • I saw this post but this looks different to the stata output I mentioned in my post. – rmuc8 Mar 31 '15 at 15:06
  • 2
    Supplement to David's comment: `addmargins(table(read$hon, read$female, dnn = c("Hon", "Female")))` – r.bot Mar 31 '15 at 15:15
  • 1
    thx @DavidArenburg , that's what I was looking for. I made an edit in the linked post that refers to the package `gmodels`. my post seems to be a duplicate then – rmuc8 Mar 31 '15 at 15:53

1 Answers1

2
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))
Sam Firke
  • 21,571
  • 9
  • 87
  • 105