reshape2: 将长格式转换为宽格式,不进行聚合

3
考虑以下内容:
ext <- data.frame(cond = rep(c('a', 'b'), each = 2), dat = runif(4) )

I want

exw <- unstack(ext, dat ~ cond)

但是出于教学目的,我想使用reshape2中的dcast()函数来完成。这个可行吗?

1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接