首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用R中的dplyr对数据帧进行二维插值

用R中的dplyr对数据帧进行二维插值
EN

Stack Overflow用户
提问于 2015-05-25 12:56:09
回答 1查看 782关注 0票数 4

我有两个数据框架:引用和内插。这是参考的一瞥。

代码语言:javascript
复制
$ Value    (dbl) 62049.67, 62040.96, 62053.02, 62039.31, 62020.82, 62001.73,...
$ X       (dbl) -10.14236, -10.14236, -10.14236, -10.14236, -10.14236, -10....
$ Y       (dbl) -12.68236, -12.64708, -12.61181, -12.57653, -12.54125, -12....

这是内插的

代码语言:javascript
复制
$ X       (dbl) -10.1346, -10.0838, -10.0330, -9.9822, -9.9314, -9.8806, -9...
$ Y       (dbl) -12.6746, -12.6746, -12.6746, -12.6746, -12.6746, -12.6746,...

我想从参考文献中得到二维插值中的变量值。

我正在考虑使用akima包中的bicubic()函数,类似于bicubic(Reference$X, Reference$Y, Reference$Value, Interpolated$X, Interpolated$Y)。然而,bicubic()期望参考$值中的矩阵。

有什么简单的方法可以在2D中插入数据帧的数据,最好是使用dplyr。

EN

回答 1

Stack Overflow用户

发布于 2017-02-25 02:57:20

不知道你是否收到过这方面的答复。我在寻找同样的东西,我不得不创建我自己的功能来完成这个任务。请见下文:

代码语言:javascript
复制
interpolate <- function(x, x1, x2, y1, y2) {
  # Interpolates between two points.
  #
  # Args:
  #   x: Corresponding x value of y value to return.
  #   x1: Low x-value.
  #   x2: High x-value.
  #   y1: Low y-value.
  #   y2: High y-value.
  #
  # Returns:
  #   Interpolated value corresponding to x between the two points.
  y <- y1 + (y2-y1)*(x-x1)/(x2-x1)
  return(y)
}

doubleinterpolate <- function(x, y, z, xout, yout) {
  # Returns a double interpolated value among three vectors with two
  # values in two of the vectors.
  #
  # Args:
  #   x: Vector containing a known value.
  #   y: Vector containing a known value.
  #   z: Vector containing an unknown value.
  #   xout: Known value in x-vector.
  #   yout: Known value in y-vector.
  #
  # Returns:
  #   Double interpolated value in z of the points xout and yout.

  # Determine adjacent values in the table
  x_low <- max(x[x < xout])
  x_high <- min(x[x > xout])
  y_low <- max(y[y < yout])
  y_high <- min(y[y > yout])

  # Create df and subset
  df <- data_frame(x = x, y = y, z = z)
  df_low <- df[x == x_low, ]
  df_high <- df[x == x_high, ]

  # Interpolate low x-values
  yint1 <- as.numeric(spline(df_low$y, df_low$z, xout = yout)[2])
  yint2 <- as.numeric(spline(df_high$y, df_high$z, xout = yout)[2])

  #Interpolate to get last value
  zout <- interpolate(xout, x_low, x_high, yint1, yint2)

  return(zout)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30438889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档