[GRASS-stats] SpatialGridDataFrame simple selection problem
Roger Bivand
Roger.Bivand at nhh.no
Thu Nov 11 14:50:04 EST 2010
On Thu, 11 Nov 2010, Chris Carleton wrote:
> Hi List,
>
> I've imported a SpatialGridDataFrame (SGDF) from GRASS with spgrass, and now
> I'm trying to select only those entries with a certain value in a column.
> What is the appropriate syntax for selecting cells in a grid that have
> particular values? Here's what I have:
>
> ----------
>> summary(modelRast)
> Object of class SpatialGridDataFrame
> Coordinates:
> min max
> x 174812.5 382055.5
> y 1798172.6 1942292.8
> Is projected: TRUE
> proj4string :
> [+proj=utm +zone=16 +a=6378137 +rf=298.257223563 +no_defs
> +towgs84=0.000,0.000,0.000 +to_meter=1.0]
> Number of points: 2
> Grid attributes:
> cellcentre.offset cellsize cells.dim
> x 174820 15.00021 13816
> y 1798180 15.00002 9608
> Data attributes:
> Value 1 NA's
> 1534000 131210128
> ----------
>
> So, the SGDF obviously 'knows' which cells are NA and which are 1, but how
> do I select cells on the basis of a column value?
Firstly, this is a very large object if you are considering statistical
analysis. In general, you treat Spatial*DataFrames like any data frame. To
operate on it, you might choose to coerce it to a SpatialPixelsDataFrame,
which drops all of the missing values cells, and retains only those within
your apparent mask. Then, you'd do something like:
modelRastSP <- as(modelRast, "SpatialPixelsDataFrame")
# add a variable
modelRastSP$newvar <- whatever
The same "$" or "[[" operator works on a SpatialGridDataFrame. Are you
asking because you need to interpolate to the raster within a mask? Then
you could use coordinates(modelRastSP) to get the prediction points.
If you can make your questions more specific, I think that others may be
able to help,
Roger
>
> Also, is there a way to write values into a column in the SGDF without
> having to create a new df object and then combine them? For example, how do
> I:
>
> ---pseudo code---
> for cell in sgdf {
> thing = calculate_something(cell)
> write2grid(cell, thing)
> }
> ------------------------
>
> I haven't been able to work this out on my own or find any examples of this
> type of selection online (been searching a while... ) though I might be
> looking in the wrong places. Thanks,
>
> Chris
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the grass-stats
mailing list