Saturday 15 September 2012

r - Create new columns based on similar row values -



r - Create new columns based on similar row values -

how create new set of info frame columns based on matched row values? instance, sample info frame:

x<-data.frame(cbind(numsp=rep(c(16,64,256),each=12),colless=rep(c("loic","midic","hiic"),each=4, times=3), lambdae=rep(c(true,false),each=2,times=9),ntree=rep(c(1,2),length.out=36), metric1=seq(1:36), metric2=seq(1:36)))

for when parameter, e.g., lambdae, i'd create new columns metric1 , metric 2 based on whether lambdae true or false. info frame this:

x2<-data.frame(cbind(numsp=rep(c(16,64,256),each=6),colless=rep(c("hiic","loic","midic"),each=2, times=3), ntree=rep(c(1,2),length.out=18), metric1.lambde.false=c(11,12,3,4,7,8,35,36,27,28,31,32,23,24,15,16,19,20), metric2.lambde.false=c(11,12,3,4,7,8,35,36,27,28,31,32,23,24,15,16,19,20),metric1.lambde.true=c(9,10,1,2,5,6,33,34,25,26,29,30,21,22,13,14,17,18), metric2.lambde.true=c(9,10,1,2,5,6,33,34,25,26,29,30,21,22,13,14,17,18)))

or alternatively parameter "colless", new set of columns metric1 , metric2 each level of colless.

thanks in advance!

okay, looks library reshape2 has quick solution:

reshape(x, direction="wide", idvar=c("numsp","colless","ntree"), timevar="lambdae")

r

No comments:

Post a Comment