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.