I want to create a tables using R that contain the information, if plant species occur at specific points (coordinates). Therefore, I want to use shapefiles, containing the distribution of several specific plant species. My output has to be a table, that for every point/coordinate indicate the presence of every plant species as 1 and the absence as 0.
First, I read in my first shapefile and the CVS table, containing the coordinates I need:
plant <- shapefile ('plant.shp')
birds<-read.csv2("bird_Coordinates.csv")
Next, I extract the coordinates, save them in a dataframe and project the points over the distribution shapefile:
lats <- birds$lat
lons <- birds$lon
pts <- data.frame(x=lons,y=lats)
coordinates(pts) <- ~x+y
proj4string(pts) <- proj4string(plant)
When I now plot the shapefile and the coordinates, I see the shape of the plant distribution and two red points, which indicate that two of my about 60 points are within this distribution:
plot(plant)
points(pts, pch=20, col='red')
Next, I try to use over
, to associate the points to the distribution. Here I used two different ways:
1.
over(pts, plant)$admin
cbind.data.frame(pts, plant=over(pts, plant)$admin)
Resulting in the warning message: Error in data.frame(..., check.names = FALSE) : Arguments imply differing numbers of rows: 64, 0
2.
plantsp <- !is.na(over(pts, as(plant, "SpatialPolygons")))
pts$plant <- over(pts, plant)$Unit_Name
Resulting the the warning message: Error in validObject(.Object) : invalid class “SpatialPointsDataFrame”: number of rows in data.frame and SpatialPoints don't match
So, both possibilities fail and I do not know what I did wrong. I know, that for this distribution range, only two points are inside the range, is the the source of the trouble? And how may I fix this? I would be so thankful, if anyone could tell me, how to get this cvs Table, containing the presence/absense information for the distribution ranges for every point!