0

我有一个用于绘制序数回归模型结果的数据框。我想融化我的数据框,但结果不正确。当我从具有较少列的原始数据框创建玩具数据框时,我得到了正确的答案。知道我在这里做错了什么吗?请参阅附加的代码和输出。

> 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

这应该在熔融数据帧(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

出于某种原因,两个变量(onp 和 on)都会出现相同的值。但是当我对数据帧中的较少变量执行相同操作时,我得到:

    > 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

知道第二个代码位中的代码有什么问题吗?

编辑。@Dwin 请求。还尝试使用正式的列名而不是数字,但没有任何运气。

> 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 ...

编辑-@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 

编辑-@巴蒂斯特

> 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

编辑-解决方案@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
4

0 回答 0