我想复制一个经常用于官方统计的表,但目前为止没有成功。鉴于这样一个数据帧:频率表与R中的几个变量
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)
我想创造出每年的表,所有的学生(所有)和那些谁是女性,那些谁参加和那些谁通过的数量。请注意以下“哪个”是指所有学生。
一表我的想法会看起来像:
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])
我肯定是这种事情在R.
注意一个更好的办法:我看到了LaTex的解决方案,但我不使用这将适用于我,因为我需要在Excel中导出表。
在此先感谢
谢谢。非常感谢。我一定会学习plyr。 – user1043144 2012-08-07 19:18:53
很好的答案,但比@csgillespie晚一分钟。 – 2012-08-07 19:20:44
@Jilber,我想你的意思是*提前一分钟*。你的评论中不应该有“but”。 – A5C1D2H2I1M1N2O1R2T1 2012-08-07 19:22:51