2015-03-31 34 views
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,但由于这个问题的代码没有包含gmodelsCrossTable,我无法应用它。输出也看起来不同。

+3

'addmargins(table(read $ hon,read $ female)''? – 2015-03-31 15:03:45

+0

可能的重复[Mimic tabata命令从Stata在R](http://stackoverflow.com/questions/1304381​​7/mimic-tabulate-command-from-stata-in-r) – 2015-03-31 15:05:20

+0

我看到这篇文章,但这看起来不同我在帖子中提到的stata输出。 – rmuc8 2015-03-31 15:06:02

回答

1
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)) 

返回:

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 | 
-------------|-----------|-----------|-----------| 

指定所有这些参数是繁琐的,所以如果你发现自己使用CrossTable()功能往往你可以写一个包装函数:

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))) 
} 

说它像这样:

with(read, tab(hon, gender))