【问题标题】:R: Graphing binned dataR:绘制分箱数据
【发布时间】:2011-04-07 22:08:57
【问题描述】:

我正在将数据分类到 bin 中并进行平均,请参阅 this solution

我使用与上述链接完全相同的解决方案,但将我的数据固定为散点图。造成我困难的代码是:

myData.class <- cut(df$xaxis, seq(0,30,length=60), include.lowest=TRUE)
mean.yaxis <- tapply(df$yaxis, myData.class, mean)
lines(mean.yaxis ~ seq(0, 30, length=60))

对行的调用产生错误:

Error in model.frame.default(formula = mean.yaxis ~ seq(0, 30, length = 60),  : 
    variable lengths differ (found for 'seq(0, 30, length = 60)')

调用 str(mean.yaxis) 会产生:

num [1:59(1d)] 0 0 0 0.349 4.652 ...
- attr(*, "dimnames")=List of 1
  ..$ : chr [1:59] "[0,0.508]" "(0.508,1.02]" "(1.02,1.53]" "(1.53,2.03]" ...

如何在调用函数 lines(...) 时访问正确的数据?

【问题讨论】:

  • 如果没有您的数据,或者最好是一个小的、可重现的示例,我们的工作有点困难。
  • @Roman 我发布的链接使用了预装的地震数据集。
  • 分箱连续数据通常被认为是错误的形式。我听从弗兰克哈雷尔的意见。 biostat.mc.vanderbilt.edu/wiki/Main/CatContinuous
  • @Richie 感谢您提供的信息,我实际上正在遵循 IEC61400,所以只要按照我的指示去做。但很高兴知道。

标签: r


【解决方案1】:

您的链接中接受的答案中给出了最佳解决方案。这将在 x 轴上绘制间隔。

cut 返回一个比您的序列低 1 级的因子(如您所见)。如果你想要区间中点,你可以这样做(以前一个例子):

data(quakes)

Seq <- c(40, 120, 200, 300, 400, 500, 600, 680)
depth.class <- cut(quakes$depth, Seq, include.lowest = TRUE)
mean.mag <- tapply(quakes$mag, depth.class, mean)

class.mids <- Seq[-1] - diff(Seq)/2

plot(mean.mag~class.mids,xlim=range(Seq))
lines(mean.mag~class.mids)

【讨论】:

    【解决方案2】:

    也许:

    lines(mean.yaxis ~ seq(0, 30, length=length(mean.yaxis)))
    

    HTH

    【讨论】:

    • @Iselzer :这会将均值映射到一侧间隔的左边界,另一侧映射到右边界。所以基本上只有中间点在右边,其余的都向左或向右伸展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多