r - aggregate and transform a dataset -


i have sample dataset following:

df=data.frame(iter=c(1, 1, 2, 2), exp=c("a", "b", "a", "b"),        val=c(2.3, 3.6, 4.0, 5.0)) 

the tabular form be:

  iter exp val     1    2.3     1   b  3.6     2    4.0     2   b  5.0 

i trying transform in way group , b, add ratio column value such df$val[1]/df$val[2], df$val[3]/df$val[4], end result looks like:

   iter ratio    1    2.3/3.6    2    4.0/5.0 

i feel should job of ddply, couldn't see path done. appreciated.

try this:

library(plyr) df2 <- ddply(df, .(iter), summarise, ratio=paste(val[which(exp=="a")],"/",val[which(exp=="b")],sep="")) 

which gave me:

  iter   ratio      1 2.3/3.6      2     4/5 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -