0

I have a dataframe for plotting the results of an ordinal regression model. I´d like to melt my dataframe, but the results are not correct. When I create a toy dataframe from the original dataframe with fewer columns I get the correct answers. Any idea what I´m doing wrong here? Please see attached code and 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 be reproduced in the 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 some reason the same values occur for both variables (onp and on).. But when I do the same with fewer variables in the dataframe I get:

    > df<-diff.mod[,c(1,2,7,11,12)]
    > df2<-melt(df,measure.vars=c(4,5)) 
      #Identical to variables 11 and 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 what is wrong with the code in the second codebit??

EDIT. @Dwin request. Also tried using formal column names instead of numbers without any 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 ...
 $ as         : 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
Misha
  • 3,114
  • 8
  • 39
  • 60
  • I think we would at least need to see `str(diff.mod)`. The use of column numbers rather than column names may hide errors. – IRTFM May 19 '13 at 20:38
  • does column 8, named "value" create a problem? If so, you might want to try and specify id.vars as well as measured.vars. – baptiste May 19 '13 at 20:50
  • your edit seems to have a problem of copy+paste, but i gather the "value" name is problematic – baptiste May 19 '13 at 21:21
  • "Value" was the culprit. Thx – Misha May 19 '13 at 21:30
  • 1
    for the sake of completeness, it would be nice if you could re-write your question with a minimal example including dummy data, so that a real answer can be given below, that might help others in the future. BTW I'm sure there's a better solution than renaming your variables. – baptiste May 19 '13 at 21:53
  • 1
    @Misha, as suggested by baptise, please write up your solution with sample data and mark the answer as "accepted". This would help future users clearly see that the issue has been resolved. – A5C1D2H2I1M1N2O1R2T1 Aug 02 '13 at 04:31

0 Answers0