从“预磨"的表面创建曲面点数 [英] Create a surface from "pre-gridded" points
问题描述
我有一个很大的data.frame
,其中有3个变量Longitude
,Latitude
和Temp
.
I have a large data.frame
which has 3 variables Longitude
, Latitude
and Temp
.
对数据进行排列,以使其在1/4度的网格"上有规律地间隔开-以便dput(head(dat))
给出:
The data is arranged so that it is regularly spaced on a "grid" of 1/4 degree - so that dput(head(dat))
gives:
structure(list(Longitude = c(0.125, 0.375, 0.625, 0.875, 1.125,
1.375), Latitude = c(0.125, 0.125, 0.125, 0.125, 0.125, 0.125
), Temp = c(25.2163, 25.1917, 25.1593, 25.125, 25.0908, 25.0612
)), .Names = c("Longitude", "Latitude", "Temp"), row.names = c(NA,
6L), class = "data.frame").
我在将其重新排列为所需格式时遇到问题.
I am having problems re-arranging it to the required format.
我想创建一个常规曲面对象(通常是一个列表),其中x和y是网格值,z是该表面的对应矩阵.这是persp
,contour
,image
等使用的常用格式.
I would like to create a regular surface object (typically a list), where x and y are the grid values and z is a corresponding matrix of the surface. This is the usual format used by persp
, contour
, image
etc.
使用这个表面对象,我将能够使用fields
包中的interp.surf
轻松地将其插值到位置矩阵.
Using this surface object I will could then be able to easily interpolate to a matrix of locations using interp.surf
from the fields
package.
任何建议都会很棒.
推荐答案
假设您的数据就像
set.seed(123)
d <- data.frame(lon=rep(seq(0,1,0.25), times=5),
lat=rep(seq(0,1,0.25), each=5),
temp=sample(1:25, 25, replace=TRUE))
head(d, 8)
# lon lat temp
# 1 0.00 0.00 8
# 2 0.25 0.00 20
# 3 0.50 0.00 11
# 4 0.75 0.00 23
# 5 1.00 0.00 24
# 6 0.00 0.25 2
# 7 0.25 0.25 14
# 8 0.50 0.25 23
我们创建一个z
矩阵,该矩阵表示网格中每个点的值.然后,我们将网格线(x
和y
)的位置以及z
一起放入列表.
We create a z
matrix that represent the values for each point in the grid. We then put the locations of the grid lines (x
and y
) into a list, together with z
.
library(reshape2)
z <- acast(d, lat~lon, value.var="temp")
X <- list(x=sort(unique(d$lon)),
y=sort(unique(d$lat)),
z=z)
image(X, col=gray.colors(25))
with(d, text(lon, lat, labels=temp))
另请参见更改纬度& Lon向量到R 中的矩阵.
这篇关于从“预磨"的表面创建曲面点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!