I need to 'rasterize' multiple attributes of a SpatialPolygonsDataframe. Then, I will multiply the values in each raster layer by the values of a given vector (e.g. 'gp'). I am using the wrld_simpl dataset from maptools for a start. wrld_simpl has an attribute called POP2005 and I will add more attrributes corresponding to population estimates for the years (2010:2100). I'm trying to write a loop or function so that I don't have to manually rasterize each attribute, then calculate their new values independently, and then repeat all these steps once and again.
data(wrld_simpl)
gp <- seq(1,246)
myraster <- raster(nrow = 572, ncol = 1440, xmn = -180, xmx = 180, ymn= -58, ymx = 85)
Because the rasterize() function does not allow me to select a specific attribute from the spatialPolygonsDataframe, I have used raster:::.polygonsToRaster() instead (which does exactly the same) to convert my attributes to a raster layer.
rastergp <- raster:::.polygonsToRaster(wrld_simpl, myraster, field = wrld_simpl$POP2005)
stackraster <- stack(rastergp, gp)
estimation <- calc(stackraster, fun = function(x) x[1]*x[2])
Does anyone have any suggestions about how to proceed? Thanks a lot!