reshape2:dcast很高,没有聚合

时间:2012-11-19 15:58:00

标签: r reshape2

考虑

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

我想要

exw <- unstack(ext, dat ~ cond)

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

1 个答案:

答案 0 :(得分:5)

您必须告诉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