R中具有多个变量的频率表 [英] frequency table with several variables in R
问题描述
我正在尝试复制一个经常用于官方统计的表格,但到目前为止没有成功。给定这样的数据帧:
I am trying to replicate a table often used in official statistics but no success so far. Given a dataframe like this one:
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)
我想创建一个表格,显示每年,所有学生(全部)和女性人数,参与人数以及谁过去了请注意,下面的其中指所有学生。
I would like to create a table showing PER YEAR , the number of all students (all) and those who are female, those who participated and those who passed. Please note "ofwhich" below refers to all students.
我想到的一张表格如下:
A table I have in mind would look like that:
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中有一种更好的方法可以解决这种问题。
I am sure there is a better way to this kind of thing in R.
注意:我已经看过LaTex解决方案,但是我不使用它会起作用对我来说,因为我需要在Excel中导出表。
Note: I have seen LaTex solutions, but I am not use this will work for me as I need to export the table in Excel .
预先感谢
推荐答案
使用 plyr
:
require(plyr)
ddply(d1, .(ExamenYear), summarize,
All=length(ExamenYear),
participated=sum(participated=="yes"),
ofwhichFemale=sum(StudentGender=="F"),
ofWhichPassed=sum(passed=="yes"))
其中给出:
ExamenYear All participated ofwhichFemale ofWhichPassed
1 2007 3 2 2 2
2 2008 4 3 2 3
3 2009 3 3 0 2
这篇关于R中具有多个变量的频率表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!