你也許可以使用「空間」方法? 可視化你的數據作爲空間對象,您的問題將變得刪除你的補丁的邊界...
這可以非常直接地使用包raster
來完成:因此找到boundaries
和mask
您的數據。
library(dplyr)
library(raster)
# Your reproducible example
myDF = rbind(so,so1,so2,so3)
myDF$z = 1 # there may actually be more 'z' variables
# Rasterize your data
r = rasterFromXYZ(myDF) # if there are more vars, this will be a RasterBrick
par(mfrow=c(2,2))
plot(r, main='Original data')
# Here I artificially add 1 row above and down and 1 column left and right,
# This is a trick needed to make sure to also remove the cells that are
# located at the border of your raster with `boundaries` in the next step.
newextent = extent(r) + c(-res(r)[1], res(r)[1], -res(r)[2], res(r)[2])
r = extend(r, newextent)
plot(r, main='Artificially extended')
plot(rasterToPoints(r, spatial=T), add=T, col='blue', pch=20, cex=0.3)
# Get the cells to remove, i.e. the boundaries
bounds = boundaries(r[[1]], asNA=T) #[[1]]: in case r is a RasterBrick
plot(bounds, main='Cells to remove (where 1)')
plot(rasterToPoints(bounds, spatial=T), add=T, col='red', pch=20, cex=0.3)
# Then mask your data (i.e. subset to remove boundaries)
subr = mask(r, bounds, maskvalue=1)
plot(subr, main='Resulting data')
plot(rasterToPoints(subr, spatial=T), add=T, col='blue', pch=20, cex=0.3)
# This is your new data (the added NA's are not translated so it's OK)
myDF2 = rasterToPoints(subr)
它會幫助你?
來源
2017-03-01 10:42:18
ztl
我猜你不能在你想要柵格化的數據框中有任何其他變量?只是x和y? – Mateusz1981
實際上你可能有很多變量:-)'rasterFromXYZ'然後會產生一個'RasterBrick'(即一個多層柵格,每個變量在你的data.frame中有一層)。唯一的區別是你需要做'bounds = boundaries(r [[1]],asNA = T)'作爲'邊界'不適用於'RasterBrick'(所以你選擇第一個層來處理它 - 這意味着你的所有變量必須具有相同的空間分佈)。我相應地編輯了答案:它現在對任何情況都有效(1個或多個變量)。 – ztl