我有海洋物种的位置数据。但是,有些数据落在 Land 上,我想删除它们而不找到它们所属的行使用 (PS2<-PS1[-c(1,2,3),]
),因为我有一个大型数据集。我在寻找线索,但真的找不到好东西。 谢谢。
这是我的代码
require(sp)
library(maptools)
require(raster)
data(wrld_simpl)
PS<-read.csv('Test.csv')
PS
#-----------------
lat lon
1 -32.98000 154.000000
2 36.94625 8.212916
3 -37.81430 -57.479584
4 -19.77236 -40.019028
5 -25.70459 -48.473195
6 -22.47125 -41.859027
7 -28.08153 -48.627082
8 10.56000 39.090000
9 50.50000 50.600000
10 52.50000 5.700000
PS1 <- subset(PS, !is.na(lon) & !is.na(lat))
plot(wrld_simpl, bg='azure2', col='khaki', border='#AAAAAA')
#restore the box around the map
box()
#plot points
points(PS1$lon, PS1$lat, col='orange', pch=20, cex=0.75)
# plot points again to add a border, for better visibility
points(PS1$lon, PS1$lat, col='red', cex=0.75)
The last three data fall on land.
这显示了如何使用%over%
(您也可以使用 over
函数
library(sp)
library(raster)
library(maptools)
data(wrld_simpl)
PS <- matrix( c(-32.98000, 154.000000, 36.94625, 8.212916,-37.81430, -57.479584,-19.77236, -40.019028,-25.70459, -48.473195,-22.47125, -41.859027,-28.08153, -48.627082, 10.56000, 39.090000,50.50000, 50.600000, 52.50000, 5.700000), ncol=2, byrow=TRUE)
ps <- PS[,2:1]
sp <- SpatialPoints(ps)
crs(sp) = crs(wrld_simpl)
i <- sp %over% wrld_simpl
marine <- is.na(i$ISO3)
spm <- sp[marine, ]
plot(wrld_simpl)
points(ps, pch=20, col='red')
points(spm, pch=20, col='blue')