您是对的……这很容易!“栅格”包具有一些处理创建和处理栅格的非常简单的方法。
library(maptools)
library(raster)
# Load your point shapefile (with IP values in an IP field):
pts <- readShapePoints("pts.shp")
# Create a raster, give it the same extent as the points
# and define rows and columns:
rast <- raster()
extent(rast) <- extent(pts) # this might be unnecessary
ncol(rast) <- 20 # this is one way of assigning cell size / resolution
nrow(rast) <- 20
# And then ... rasterize it! This creates a grid version
# of your points using the cells of rast, values from the IP field:
rast2 <- rasterize(pts, rast, pts$IP, fun=mean)
您可以通过多种方式分配栅格大小和分辨率-仔细查看栅格数据包文档。
在上面的示例中,可以使用函数“均值”来计算来自栅格化的栅格像元的值。请确保将其放入:否则,它将仅使用从出现的最后一点起的IP值!
从CSV:
pts <- read.csv("IP.csv")
coordinates(pts) <- ~lon+lat
rast <- raster(ncol = 10, nrow = 10)
extent(rast) <- extent(pts)
rasterize(pts, rast, pts$IP, fun = mean)