Skip to content
Back to formatted view

Raw Message

Message-ID: <CB48FBE34FD7F64D8FFDBB410C26CF750138C72B8E09@EXCH07-VS1.spu.local>
Date: 2011-10-13T18:59:27Z
From: Bailey, Daniel
Subject: getting data associated with coordinates in a spatial data frame
In-Reply-To: <CAM_vjuk_B=0R1_q-nYYYk_dE85H7y9Cyt-03ebQK5O93XAm0dw@mail.gmail.com>

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