Learn R Programming

disaggregation (version 0.1.4)

getPolygonData: Extract polygon id and response data into a data.frame from a SpatialPolygonsDataFrame

Description

Returns a data.frame with a row for each polygon in the SpatialPolygonDataFrame and columns: area_id, response and N, containing the id of the polygon, the values of the response for that polygon, and the sample size respectively. If the data is not survey data (the sample size does not exist), this column will contain NAs.

Usage

getPolygonData(
  shape,
  id_var = "area_id",
  response_var = "response",
  sample_size_var = NULL
)

Value

A data.frame with a row for each polygon in the SpatialPolygonDataFrame and columns: area_id, response and N, containing the id of the polygon, the values of the response for that polygon, and the sample size respectively. If the data is not survey data (the sample size does not exist), this column will contain NAs.

Arguments

shape

A SpatialPolygons object containing response data.

id_var

Name of column in shape object with the polygon id. Default 'area_id'.

response_var

Name of column in shape object with the response data. Default 'response'.

sample_size_var

For survey data, name of column in SpatialPolygonDataFrame object (if it exists) with the sample size data. Default NULL.

Examples

Run this code
{
 polygons <- list()
 for(i in 1:100) {
   row <- ceiling(i/10)
   col <- ifelse(i %% 10 != 0, i %% 10, 10)
   xmin = 2*(col - 1); xmax = 2*col; ymin = 2*(row - 1); ymax = 2*row
   polygons[[i]] <- rbind(c(xmin, ymax), c(xmax,ymax), c(xmax, ymin), c(xmin,ymin))
 }

 polys <- do.call(raster::spPolygons, polygons)
 response_df <- data.frame(area_id = 1:100, response = runif(100, min = 0, max = 10))
 spdf <- sp::SpatialPolygonsDataFrame(polys, response_df)

 getPolygonData(spdf, id_var = 'area_id', response_var = 'response')
}


Run the code above in your browser using DataLab