[R-sig-Geo] Strategies for modifying country-specific grids from a global gridded dataset in simple matrix form?

andy south southandy at gmail.com
Mon Dec 13 13:50:31 CET 2010


Hi Jooil,
The code below should help with your query about identifying the
countrys that grid cells fall in using rworldmap.
All the best,
Andy


#testing identifying the country of grid cells of any resolution (centroids)
#plus aggregating a grid of any dimension to countries

library(rworldmap)

#creating a toy 1 degree grid
gridTopology <- GridTopology(c(-180,-90), c(1,1), c(360,180))
gridVals <- data.frame(att=c(1:64800))
sGDF <- SpatialGridDataFrame(gridTopology,data=gridVals)
#test plotting the grid
mapDevice()
mapGriddedData(sGDF)

#get the country map from rworldmap
sPDF <- getMap()
str(sPDF at data)

#overlay the country map on the grid
#gives a vector for each grid cell giving the index of the country in the sPDF
#int [1:64800]
countryIndexPerGrid <- overlay(sGDF,sPDF)
#to get the UN country code per grid cell
codeUNPerGrid <- sPDF$UN[ countryIndexPerGrid ]
#add that on to the sGDF
sGDF$UN <- codeUNPerGrid

#to manipulate one countries cells in the grid
#(following query 20101207)
#UN country code for China is 156
sGDF$att[which(sGDF$UN == 156)] <- 0

#to show that grid values for China have been replaced by 0
mapDevice()
mapGriddedData(sGDF)

#note that you can also then aggregate all of the gridded data to a
country level

#aggregate cells by UN countrycode (into a column called x)
dFbyCountry <- aggregate(sGDF$att
                     , by=list(UN = sGDF$UN)
                     , FUN = 'mean'
                     , na.rm=TRUE )

#join aggregated data to the country map
sPDF2 <- joinCountryData2Map(dFbyCountry
              , joinCode = "UN"
              , nameJoinColumn = "UN")

#plots the country map and does show that countries further south get
higher values
mapDevice()
mapCountryData(sPDF2,nameColumnToPlot='x')

Message: 12
Date: Mon, 6 Dec 2010 11:53:48 +0900
From: Jooil Kim <kji2080 at gmail.com>
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Strategies for modifying country-specific grids
       from a global gridded dataset in simple matrix form?
Message-ID:
       <AANLkTinDNeLAzbPCV8AHrwKseVeUmwN_fBynXtwk84qV at mail.gmail.com>
Content-Type: text/plain

Hello all,

I've just started working with gridded data in R, mostly in using the
"rworldmap" package.

So far I have been able to make ASCIIgrid files of global datasets that I
imported into R as simple matrices (ex. 360x180 matrix with data values
corresponding to each of the 1x1 grid), and draw them with functions
included in "rworldmap".

My problem now is in manipulating parts of these datasets, specifically with
regards to country.

For example, from the global matrix I want to subset just the grids that
belong to China and multiply a factor to those Chinese grids, or maybe make
them all 0s.

Selecting all the grids that belong to a country seems straightforward in
theory, but being the novice that I am, I can't figure out an effective way
to do this.

I've also been thinking that I should probably switch over my simple
matrices to sp dataframes, so that the spatial aspects of my dataset can be
better handled within R, but so far have been hesitant to do so because the
benefits of such a change have not been apparent.


Can I get some suggestions or help on how I should proceed?

Thanks in advance,

Jooil

--
#############################################
Jooil Kim
Graduate Student
School of Earth and Environmental Sciences,
Seoul National University
Gwanak Gu Shillim 9 Dong San 56-1
Seoul National Univ. Bld#501, Rm 503
Seoul, Rep. of Korea 151-742
kji2080 at gmail.com
tel) 82-2-877-6741
fax) 82-2-885-7164
#############################################



More information about the R-sig-Geo mailing list