[英]Interpolate in 2D with data from a data frame using dplyr in R
I have two data frames: Reference and Interpolated . 我有两个数据框: 参考和插值 。 This is the glimpse() of Reference :
这是参考文献的一瞥():
$ 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....
And this is Interpolated : 这是插值 :
$ 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,...
I want to obtain the variable Value in Interpolated using a 2D interpolation from Reference . 我想使用Reference中的2D插值获得变量值 Inter inlated 。
I was thinking about employing the bicubic() function from the akima package, something like bicubic(Reference$X, Reference$Y, Reference$Value, Interpolated$X, Interpolated$Y)
. 我正在考虑使用akima包中的bicubic()函数,比如
bicubic(Reference$X, Reference$Y, Reference$Value, Interpolated$X, Interpolated$Y)
。 However bicubic() expects a matrix in Reference$Value . 但是, bicubic()需要Reference $ Value中的矩阵。
Is there any easy way to interpolate in 2D with data from a data frame preferably using dplyr ? 有没有简单的方法在2D中插入数据帧中的数据,最好是使用dplyr ?
Don't know if you ever received an answer to this. 不知道你是否收到过这个答案。 I was looking for the same thing and had to create my own functions to do this.
我一直在寻找相同的东西,不得不创建自己的功能来做到这一点。 Please see below:
请看下面:
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.