How to convert latitude and longitude values into grid cells from a shapefile in R?

Hi there!

I have a data frame with geo-located events. I want to convert each geo-location (i.e. the values of the variables latitude and longitude in each observation) into the equivalent grid cell from a shapefile (i.e. PRIO-Grids).

I tried the following code:

# 1. Combine the longitude and latitude
coordinates <- cbind(df$longitude, df$latitude))

# 2. Transform into spatial points
sp_points <- SpatialPoints(coordinates)

# 3. Upload shapefile
prio_grids_shapefile <- shapefile(C:\path\to\shapefile.shp)

# 4. Transform spatial points into grids
geo$prio_grids <- spTransform(sp_points, CRS(proj4string(prio_grids_shapefile)))

I get an error right away on step 2:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’

I confess I am clueless here, first time using GIS packages in R. I appreciate any help!

Thanks a lot!

Actually I found a different solution. I converted the shapefile into .csv and worked it out from there. It was more intuitive for me to work with another data frame and I could eventually find my way out.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.