2012-11-19 37 views
3

考慮reshape2:dcast高大寬進不聚集

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

我想

exw <- unstack(ext, dat ~ cond) 

但我想用dcast()做到在reshape2(用於教育的目的)。這可能嗎?

回答

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