I have a point data (Lon and Lat), and I would like to create presence and absence data from it. How can I do this in R? Example is below.
Load data:
df <- read.table(text=
"species lon lat
Oncorhynchus_kisutch -130.25 55.75
Oncorhynchus_kisutch -129.75 55.75
Oncorhynchus_kisutch -130.25 55.25
Oncorhynchus_kisutch -129.75 55.25
Oncorhynchus_kisutch -129.25 55.25
Oncorhynchus_kisutch -133.25 54.75
Oncorhynchus_kisutch -131.75 54.75
Oncorhynchus_kisutch -131.25 54.75
",
header=TRUE
)
head(df)
# create grid
reso = 0.25
xs <- seq(-180, 180, by=reso)
ys <- seq(-90, 90, by=reso)
grd <- expand.grid(
x=xs,
y=ys,
presence=0
)
head(grd)
# query
for(i in seq(nrow(df))){
tmp <- which(df$lon[i] == grd$x & df$lat[i] == grd$y)
if(length(tmp)>0){
grd$presence[tmp] <- 1
}
}
png("plot.png", width=5, height=5, units="in", res=600, type="cairo")
plot(grd$x, grd$y, pch=1, cex=1, col=c(NA, 1)[grd$presence+1], lwd=0.5)
dev.off()
However, this code doesn't work for me when I use irregular Lon and Lat data like this:
Lat Lon
24.08333 32.88333
30.00486 31.23571
32.5 27.5
33.333 -8.417
34.395 26.05383
34.5 -6.917
35 15
35.0738 32.3335
35.19509 25.71726
35.2047 25.7221
35.26665 26.22368
35.33198 25.39159