Note that there are some explanatory texts on larger screens.

plurals
  1. POMelt in reshape2
    primarykey
    data
    text
    <p>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.</p> <pre><code>&gt; fivenum(diff.mod[,11]) [1] -2.250835e-06 -2.558362e-07 -3.719817e-08 1.670986e-07 2.644583e-06 &gt; fivenum(diff.mod[,12]) [1] -0.237450499 -0.021690233 0.001226833 0.019041952 0.277317128 </code></pre> <p>This should be reproduced in the molten dataframe (diff.mod.graf)</p> <pre><code>&gt; diff.mod.graf&lt;-melt(diff.mod,measure.vars=c(11,12)) &gt; 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 </code></pre> <p>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:</p> <pre><code> &gt; df&lt;-diff.mod[,c(1,2,7,11,12)] &gt; df2&lt;-melt(df,measure.vars=c(4,5)) #Identical to variables 11 and 12 above &gt; 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 </code></pre> <p>Any idea what is wrong with the code in the second codebit??</p> <p>EDIT. @Dwin request. Also tried using formal column names instead of numbers without any luck.</p> <pre><code>&gt; 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 ... </code></pre> <p>Edit- @Baptiste </p> <pre><code>&gt; diff.mod.graf&lt;-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op")) &gt; 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 </code></pre> <p>EDIT- @ Baptiste</p> <pre><code>&gt; diff.mod.graf&lt;-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op")) &gt; 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 </code></pre> <p>Edit- SOLUTION @Baptiste</p> <pre><code> &gt;names(diff.mod)[8]&lt;-"J" &gt; diff.mod.graf&lt;-melt(diff.mod,id.vars=c(1:10),measure.vars=c("onp","op")) &gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload