[R] getting data associated with coordinates in a spatial data frame

Sarah Goslee sarah.goslee at gmail.com
Thu Oct 13 20:53:54 CEST 2011


On Thu, Oct 13, 2011 at 2:44 PM, Bailey, Daniel <bailed at spu.edu> wrote:
> Michael, that's half of the problem solved (whew!!). Now how do I change the data at that location?

You assign it a new value, just as for any assignment. Using the
example from my previous email:


> data(meuse.grid)
> m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data = meuse.grid)
>
> m at data[coordinates(m)[,"x"] == 181100 & coordinates(m)[,"y"] == 333660,]
       x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    1     1
>
> m at data[coordinates(m)[,"x"] == 181100 & coordinates(m)[,"y"] == 333660, "soil"] <- 5
>
> m at data[coordinates(m)[,"x"] == 181100 & coordinates(m)[,"y"] == 333660,]
       x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    5     1
>


> This is not an intuitive way to manipulate data.

That's not what it's *for*. SGDFs are for storing and working with
spatial data, where all the components are needed for the spatial
reference. If you need to manipulate a lot of things, you're better
off doing it before you construct the SGDF, or you can cheat by
extracting the data slot, working with it, then reassigning it as a
single unit.

mydata <- m at data
# do stuff
m at data <- mydata

You might also benefit from reading "Applied Spatial Data Analysis
with R" by Bivand et al.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list