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

Bailey, Daniel bailed at spu.edu
Thu Oct 13 20:59:27 CEST 2011


Woohoo! Thank you Sarah and Michael. You are rock stars! 

Daniel 

-----Original Message-----
From: Sarah Goslee [mailto:sarah.goslee at gmail.com] 
Sent: Thursday, October 13, 2011 11:54 AM
To: Bailey, Daniel
Cc: r-help at r-project.org
Subject: Re: [R] getting data associated with coordinates in a spatial data frame

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