【问题标题】:frequency table with several variables in RR中有几个变量的频率表
【发布时间】:2012-08-04 20:21:29
【问题描述】:

我正在尝试复制官方统计数据中经常使用的表格,但到目前为止没有成功。给定一个像这样的数据框:

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)

我想创建一个表格,显示 PER YEAR 、所有学生(全部)和女性人数、参与人数和通过人数。请注意以下“其中”是指所有学生。

我心目中的表格应该是这样的:

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 中导出表格。

提前致谢

【问题讨论】:

    标签: r aggregate frequency


    【解决方案1】:

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

    【讨论】:

    • 谢谢。非常感谢。我肯定会学习 plyr。
    • 不错的答案,但比@csgillespie 晚一分钟。
    • @Jilber,我想你的意思是提前一分钟。您的评论中不应有“但是”。
    【解决方案2】:

    plyr 包非常适合这种事情。先加载包

    library(plyr)
    

    然后我们使用ddply函数:

    ddply(d1, "ExamenYear", summarise, 
          All = length(passed),##We can use any column for this statistics
          participated = sum(participated=="yes"),
          ofwhichFemale = sum(StudentGender=="F"),
          ofwhichpassed = sum(passed=="yes"))
    

    基本上,ddply 需要一个数据帧作为输入并返回一个数据帧。然后我们通过ExamenYear 拆分输入数据帧。在每个子表上,我们计算一些汇总统计信息。请注意,在 ddply 中,我们在引用列时不必使用 $ 表示法。

    【讨论】:

      【解决方案3】:

      可能对您的代码进行了一些修改(使用with 来减少df$ 调用的数量,并使用字符索引来改进自我文档),以使其更易于阅读并成为有价值的竞争对手到ddply 解决方案:

      with( d1, cbind(All = table(ExamenYear),
        participated      = table(ExamenYear, participated)[,"yes"],
        ofwhichFemale     = table(ExamenYear, StudentGender)[,"F"],
        ofwhichpassed     = table(ExamenYear, passed)[,"yes"])
           )
      
           All participated ofwhichFemale ofwhichpassed
      2007   3            2             2             2
      2008   4            3             2             3
      2009   3            3             0             2
      

      我希望这比 ddply 解决方案快得多,尽管只有在处理更大的数据集时才会明显。

      【讨论】:

        【解决方案4】:

        您可能还想看看 plyr 的下一个迭代器:dplyr

        它使用类似于 ggplot 的语法,并通过用 C++ 编写关键部分来提供快速性能。

        d1 %.% 
        group_by(ExamenYear) %.%    
        summarise(ALL=length(ExamenYear),
                  participated=sum(participated=="yes"),
                  ofwhichFemale=sum(StudentGender=="F"),
                  ofWhichPassed=sum(passed=="yes"))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-01
          • 2017-06-16
          • 2016-11-18
          • 1970-01-01
          • 1970-01-01
          • 2012-08-25
          相关资源
          最近更新 更多