r - Melt in reshape2 -


i have dataframe plotting results of ordinal regression model. i´d melt dataframe, results not correct. when create toy dataframe original dataframe fewer columns correct answers. idea i´m doing wrong here? please see attached code , output.

> fivenum(diff.mod[,11]) [1] -2.250835e-06 -2.558362e-07 -3.719817e-08  1.670986e-07  2.644583e-06 > fivenum(diff.mod[,12]) [1] -0.237450499 -0.021690233  0.001226833  0.019041952  0.277317128 

this should reproduced in molten dataframe (diff.mod.graf)

> diff.mod.graf<-melt(diff.mod,measure.vars=c(11,12)) > by(data=diff.mod.graf,indices=diff.mod.graf$variable,                         function(x) fivenum(x$value)) diff.mod.graf$variable: onp [1] 0.0002978989 0.0675580767 0.1793394876 0.6058061575 0.9984103955 ---------------------------------------------------------------- diff.mod.graf$variable: op [1] 0.0002978989 0.0675580767 0.1793394876 0.6058061575 0.9984103955 

for reason same values occur both variables (onp , on).. when same fewer variables in dataframe get:

    > df<-diff.mod[,c(1,2,7,11,12)]     > df2<-melt(df,measure.vars=c(4,5))        #identical variables 11 , 12 above     > by(data=df2,indices=df2$variable,function(x) fivenum(x$value))     df2$variable: onp     [1] -2.250835e-06 -2.558362e-07 -3.719817e-08  1.670986e-07  2.644583e-06    ----------------------------------------------------------------------     df2$variable: op     [1] -0.237450499 -0.021690233  0.001226833  0.019041952  0.277317128 

any idea wrong code in second codebit??

edit. @dwin request. tried using formal column names instead of numbers without luck.

> str(diff.mod) 'data.frame':   73200 obs. of  12 variables:  $ ao         : num  0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 ...  $         : factor w/ 5 levels "bo 1","bo 2",..: 1 1 1 1 1 1 1 1 1 1 ...  $ age        : num  0 0 0 0 0 0 0 0 0 0 ...  $ sex        : factor w/ 2 levels "female","male": 1 1 1 1 1 1 1 1 1 1 ...  $ sx         : factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...  $ hy         : factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...  $ yu         : factor w/ 3 levels "fit.[ 13, 29)",..: 1 1 1 1 1 1 1 1 1 1 ...  $ value      : num  0.0461 0.0477 0.0492 0.0509 0.0526 ...  $ non.prop   : num  0.0461 0.0477 0.0492 0.0509 0.0526 ...  $ prop       : num  0.0466 0.0479 0.0493 0.0506 0.0521 ...  $ onp        : num  7.84e-08 7.37e-08 6.87e-08 6.32e-08 5.73e-08 ...  $ op         : num  -4.86e-04 -2.60e-04 -2.09e-05 2.32e-04 5.00e-04 ... 

edit- @baptiste

> diff.mod.graf<-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op")) > by(data=diff.mod.graf,indices=diff.mod.graf$variable,function(x) fivenum(x$value)) diff.mod.graf$variable: onp [1] 0.0002978989 0.0675580767 0.1793394876 0.6058061575 0.9984103955 ------------------------------------------------------------------------------------------------------------------------------------------------------------  diff.mod.graf$variable: op [1] 0.0002978989 0.0675580767 0.1793394876 0.6058061575 0.9984103955  

edit- @ baptiste

> diff.mod.graf<-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op")) > by(data=diff.mod.graf,indices=diff.mod.graf$variable,function(x) fivenum(x$value)) diff.mod.graf$variable: onp [1] -2.250835e-06 -2.558362e-07 -3.719817e-08  1.670986e-07  2.644583e-06 ------------------------------------------------------------------------------------------------------------------------------------------------------------  diff.mod.graf$variable: op [1] -0.237450499 -0.021690233  0.001226833  0.019041952  0.277317128 

edit- solution @baptiste

    >names(diff.mod)[8]<-"j"     > diff.mod.graf<-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op"))     > by(data=diff.mod.graf,indices=diff.mod.graf$variable,function(x) fivenum(x$value))     diff.mod.graf$variable: onp     [1] -2.250835e-06 -2.558362e-07 -3.719817e-08  1.670986e-07  2.644583e-06     ------------------------------------------------------------------------------------     diff.mod.graf$variable: op     [1] -0.237450499 -0.021690233  0.001226833  0.019041952  0.277317128 


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -