Skip to content
Prev 274374 / 398506 Next

getting data associated with coordinates in a spatial data frame

Michael, that's half of the problem solved (whew!!). Now how do I change the data at that location?

This is not an intuitive way to manipulate data.

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

Ah yes, my eternal nemesis the S4 class...

You were basically there with

e[e$coordinates==(0,17),]

but for some access stuff that comes from the SpatialDataPointsFrame class.

You'll probably want to do this in two steps:

coords = coordinates(e)
## Use the access function coordinates to get a 2xn matrix of coordinates x and y; ## you could also do this with coords = e at coords as Weidong noted but it's discouraged

e[coords[,"x"] == my.x & coords[,"y"] == my.y, "leachate"]

I can't test it on your data, but I think this will do it.

data(meuse.grid)
coordinates(meuse.grid) <- ~x+y
M = meuse.grid

coords = coordinates(M)
 M[(coords[,"x"] == 179220) & (coords[,"y"] == 329620), "soil"]

Hope this helps,

Michael
On Thu, Oct 13, 2011 at 2:18 PM, Bailey, Daniel <bailed at spu.edu> wrote: