【问题标题】:reshape2: dcast tall into wide without aggregatingreshape2:dcast高到宽而不聚合
【发布时间】:2012-11-19 15:57:59
【问题描述】:

考虑

ext <- data.frame(cond = rep(c('a', 'b'), each = 2), dat = runif(4) )

我想要

exw <- unstack(ext, dat ~ cond)

但我想在reshape2 中使用dcast()(出于教学目的)。这可能吗?

【问题讨论】:

    标签: r reshape2


    【解决方案1】:

    你必须告诉dcast有一个识别行ID:

    例如:

    dcast(ext, 1:2~cond)
      1:2         a         b
    1   1 0.5706567 0.4360110
    2   2 0.0305229 0.7032459
    

    而且,更一般地说:

    ext$id <- sequence(rle(as.character(ext$cond))$lengths)
    dcast(ext, id~cond, value.var="dat")
    
      id         a         b
    1  1 0.5706567 0.4360110
    2  2 0.0305229 0.7032459
    

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多