Hello Andy,

Thank you very much for your examples.

 I actually ended up creating very similar code, but instead directly
importing national border information from CIESIN, which turned into quite a
hack as I was trying to translate a very detailed map into 1x1, and while
things do seem to work, I can't be sure whether I've got things completely
right or not...

Your solution of using spatial gridded data frames seem so much more elegant
than the rather primitive method I had to resort to with basic numeric
matrixes, and really was exactly the examples I was looking for, I must say.

Thanks again,

Jooil

On Mon, Dec 13, 2010 at 9:50 PM, andy south <southandy@gmail.com> wrote:

> 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@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@gmail.com>
> To: r-sig-geo@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@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@gmail.com
> tel) 82-2-877-6741
> fax) 82-2-885-7164
> #############################################
>



-- 
#############################################
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@gmail.com
tel) 82-2-877-6741
fax) 82-2-885-7164
#############################################

	[[alternative HTML version deleted]]

