use*_*144 7 aggregate r frequency
我试图复制官方统计中经常使用的表,但到目前为止没有成功.给定像这样的数据帧:
d1 <- data.frame( StudentID = c("x1", "x10", "x2",
"x3", "x4", "x5", "x6", "x7", "x8", "x9"),
StudentGender = c('F', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'M', 'M'),
ExamenYear = c('2007','2007','2007','2008','2008','2008','2008','2009','2009','2009'),
Exam = c('algebra', 'stats', 'bio', 'algebra', 'algebra', 'stats', 'stats', 'algebra', 'bio', 'bio'),
participated = c('no','yes','yes','yes','no','yes','yes','yes','yes','yes'),
passed = c('no','yes','yes','yes','no','yes','yes','yes','no','yes'),
stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
我想创建一个表格,显示每年,所有学生(所有)和女性,参与者和通过的人数.请注意下面的"ofwhich"指的是所有学生.
我想到的一张桌子看起来像这样:
cbind(All = table(d1$ExamenYear),
participated = table(d1$ExamenYear, d1$participated)[,2],
ofwhichFemale = table(d1$ExamenYear, d1$StudentGender)[,1],
ofwhichpassed = table(d1$ExamenYear, d1$passed)[,2])
Run Code Online (Sandbox Code Playgroud)
我相信在R.这种事情有更好的方法.
注意:我已经看过LaTex解决方案,但我没有使用这对我有用,因为我需要在Excel中导出表.
提前致谢
使用plyr:
require(plyr)
ddply(d1, .(ExamenYear), summarize,
All=length(ExamenYear),
participated=sum(participated=="yes"),
ofwhichFemale=sum(StudentGender=="F"),
ofWhichPassed=sum(passed=="yes"))
Run Code Online (Sandbox Code Playgroud)
这使:
ExamenYear All participated ofwhichFemale ofWhichPassed
1 2007 3 2 2 2
2 2008 4 3 2 3
3 2009 3 3 0 2
Run Code Online (Sandbox Code Playgroud)