2
我正与加州大学洛杉矶分校sample data set交叉表类似于塔塔R中
> 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
工作,我想female
的期望的结果看起来像这样stata
输出crosstab
变量hon
:
| female
hon | male female | Total
-----------+----------------------+----------
0 | 74 77 | 151
1 | 17 32 | 49
-----------+----------------------+----------
Total | 91 109 | 200
使用R
,我试图用xtabs
> xtabs(female~hon, data = read)
hon
0 1
77 32
和reshape2
> library(reshape2)
> melt <- melt(read, id="female")
> dcast(melt, variable ~ female, sum, subset = .(variable == "hon"))
hon
0 1
77 32
和table
> table(read$hon, read$female)
0 1
0 74 77
1 17 32
但这只是理想的结果
我想的一部分包括non-female
(= male
)值并计算总数,并适当地分配名称。
我错过了一个简单的功能,在R
?
我看到这个帖子Mimic tabulate command from Stata in R,但由于这个问题的代码没有包含gmodels
的CrossTable
,我无法应用它。输出也看起来不同。
'addmargins(table(read $ hon,read $ female)''? – 2015-03-31 15:03:45
可能的重复[Mimic tabata命令从Stata在R](http://stackoverflow.com/questions/13043817/mimic-tabulate-command-from-stata-in-r) – 2015-03-31 15:05:20
我看到这篇文章,但这看起来不同我在帖子中提到的stata输出。 – rmuc8 2015-03-31 15:06:02