6
4

1 回答 1

11

既然你已经走到了这一步,为什么不melt你的tx对象和使用dcast如下:

dcast(melt(tx, id.vars=c(1, 2)), ExamenYear ~ StudentGender + variable)
#   ExamenYear F_participated F_passed M_participated M_passed
# 1       2007              1        1              1        1
# 2       2008              1        1              2        2
# 3       2009             NA       NA              3        2

但是,更直接的方法可能是melt从一开始就处理您的数据:

df.m <- melt(df, id.vars=c(1:4))
dcast(df.m, ExamenYear ~ StudentGender + variable, 
      function(x) sum(x == "yes"))
#   ExamenYear F_participated F_passed M_participated M_passed
# 1       2007              1        1              1        1
# 2       2008              1        1              2        2
# 3       2009              0        0              3        2

更新:基础 R 方法

虽然所需的代码并不“漂亮”,但在基础 R 中执行此操作也不是太难。这是一种方法:

  1. 用于从您的示例aggregate()中获取。tx

    dfa <- aggregate(cbind(participated, passed) ~ 
      ExamenYear + StudentGender, df, function(x) sum(x == "yes"))
    dfa
    #   ExamenYear StudentGender participated passed
    # 1       2007             F            1      1
    # 2       2008             F            1      1
    # 3       2007             M            1      1
    # 4       2008             M            2      2
    # 5       2009             M            3      2
    
  2. 用于从“长”reshape转换dfa为“宽”。

    reshape(dfa, direction = "wide", 
            idvar="ExamenYear", timevar="StudentGender")
    #   ExamenYear participated.F passed.F participated.M passed.M
    # 1       2007              1        1              1        1
    # 2       2008              1        1              2        2
    # 5       2009             NA       NA              3        2
    
于 2012-09-15T11:59:45.590 回答