Converting point data into gridded dataframe for histogram analysis using R?

You're right ... it is pretty easy! The "raster" package has some pretty straightforward ways of dealing with creating and manipulating rasters.

library(maptools)
library(raster)

# Load your point shapefile (with IP values in an IP field):
pts <- readShapePoints("pts.shp")

# Create a raster, give it the same extent as the points
# and define rows and columns:

rast <- raster()
extent(rast) <- extent(pts) # this might be unnecessary
ncol(rast) <- 20 # this is one way of assigning cell size / resolution
nrow(rast) <- 20

# And then ... rasterize it! This creates a grid version 
# of your points using the cells of rast, values from the IP field:
rast2 <- rasterize(pts, rast, pts$IP, fun=mean) 

You can assign grid size and resolution in a number of ways - have a good look at the raster package documentation.

The values of the raster cells from rasterize can be calculated with a function - 'mean' in the example above. Make sure you put this in: otherwise it just uses the value of IP from the last point it comes across!


From a CSV:

pts <- read.csv("IP.csv")
coordinates(pts) <- ~lon+lat
rast <- raster(ncol = 10, nrow = 10)
extent(rast) <- extent(pts)
rasterize(pts, rast, pts$IP, fun = mean)

The plotKML package has a function called vect2rast. This function basically extends the rasterize function available in the raster package. The advantage of vect2rast; however, is that it requires no input from the user's side i.e. it automatically determines the grid cell size and the bounding box based on the properties of the input data set. The grid cell size is estimated based on the density/size of features in the map (nndist function in spatstat package).

library(plotKML)
Rast2 <- vect2rast(pts)

# for large data sets use SAGA GIS:
Rast2 <- vect2rast(pts, method = "SAGA")

Tags:

R

Raster

Kriging