[R-sig-Geo] Iteratively subsample a rater and reassemble

Jonathan Greenberg jgrn at illinois.edu
Mon Mar 31 18:11:46 CEST 2014


Gordon:

To clarify, you are trying to determine a relative population density
per country, correct?  I think you should look at the ?subs function,
which will make it a lot easier to swap the country ID with the
population value -- you just need a data.frame of ID and population.
Then it seems if you create a new raster that has the population of
the country for each cell, you can simply divide the per-location
population by that raster.

--j

On Mon, Mar 31, 2014 at 12:12 AM, Gordon Holtgrieve <gholt at uw.edu> wrote:
> Hello,
>
> I have not worked with rasters much in R and am having a bit of trouble.
>
> I have a raster of global population and a second raster of countries
> identified by UN code (both at 1/2 degree resolution, but with different
> extents).  Ultimately I would like to end up with a raster where each
> country's population is rescaled to N(0,1).
>
> Can someone suggest the best way to do this?
>
> So far I have been able to mask the population raster to a given country
> and perform the rescale (with the plan to loop over countries) .  I'm stuck
> on how to "reassemble" into a complete raster with all the countries.
>
> #Code thus far -- although I'm sure there is a better way.
> rasterCountriesUN[rasterCountriesUN != 840] <- NA  #Mask countries not of
> interest
>
> rasterCountriesUN[rasterCountriesUN == 840] <- 1  #replace the UN code with
> 1
>
> cropPop2010 <- rasterCountriesUN*rasterPop2010  # crop population data
> raster
>
> cropPop2010[cropPop2010>0] <- scale(cropPop2010[cropPop2010>0]) #rescale
> cells > 0 (i.e., with people)
> # I have other calculations to do here using the scaled population data.
>
> ###??? How do I store these results and move to the next country?  Or is
> there  better way to go about this?
>
>
> Many thanks,
>
> --
> Gordon Holtgrieve, Ph.D.
> Assistant Professor
> University of Washington
> School of Aquatic & Fishery Sciences
> 1122 NE Boat Street
> Box 355020
> Seattle, WA 98105 USA
> 1-206-616-7041
> skype: gholtgrieve
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007



More information about the R-sig-Geo mailing list