【问题标题】:Interpolation function approx() in r gives error - need at least two non-NA values to interpolater 中的插值函数 approx() 给出错误 - 需要至少两个非 NA 值来插值
【发布时间】:2017-06-06 09:47:55
【问题描述】:

我在 Windows 8 机器上使用 R Studio。我正在尝试在两点之间插入一个点。

x1 = -159.9, y1 = 56.5, 
x2 = -159.9, y2 = 56.3

我正在以下列方式使用approx()函数(可重现)

approx(c(-159.9,-159.9), c(56.5,56.3), n = 3)

这给了我一个错误

Error in approx(c(-159.9, -159.9), c(56.5, 56.3), n = 3) : 
need at least two non-NA values to interpolate

它需要我提供的两个非 NA 值。

该功能在其他方面完美无缺。这就是问题所在。 如果您遇到任何此类错误,请告诉我您是如何解决的?

【问题讨论】:

  • 您的 x 和 y 坐标顺序错误。 approx 函数不能在 x 坐标相同的地方插入值,但是,这正是您目前所做的。
  • x1x2 的值相同
  • @robbertjan94 这似乎是错误的原因。我应该怎么做才能处理它,因为我不能简单地更改顺序,因为有时 y1 和 y2 也会相同。
  • 这就是我解决问题的方法。只要 x1 和 x2 相同,我就将 x 与 y 互换,然后互换列的顺序。

标签: r line gis interpolation


【解决方案1】:

来自?approx()详情

输入可以包含被删除的缺失值,所以至少 需要两个完整的 (x, y) 对(对于 method = "linear",一个 除此以外)。如果存在重复(并列)的 x 值并且 tie 是 函数将其应用于每个不同 x 值的 y 值。

【讨论】:

  • 那么当x1和x2相同的情况下我需要对y值进行插值时该怎么办呢?
  • 继续阅读同一页:Useful functions in this context include mean, min, and max. If ties = "ordered" the x values are assumed to be already ordered. The first y value will be used for interpolation to the left and the last one for interpolation to the right.
【解决方案2】:

approx 函数无法在 x 坐标相同的情况下插入值。

因此,我将按如下方式解决这个问题:

  1. 对 x 坐标相等的所有情况进行分组,并按例如中值、均值或自定义构建函数聚合它们
  2. 使用您想要的插值方案,例如approx 函数。

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2020-07-31
    • 2021-12-07
    • 2011-11-03
    相关资源
    最近更新 更多