Skip to content

getting data associated with coordinates in a spatial data frame

12 messages · Weidong Gu, Sarah Goslee, R. Michael Weylandt +2 more

#
Hi,
On Wed, Oct 12, 2011 at 3:37 PM, Bailey, Daniel <bailed at spu.edu> wrote:
That's kind of mangled, but what about:

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

(Depending on the form of your coordinates, you may also have to
invoke FAQ 7.31.)

Sarah

  
    
#
Thank you Sarah. I tried your suggestion, and if I coerce it into a normal data.frame, that method works. But if you've already made the data into a SpatialPixelsDataFrame and run coordinates (both from the package "sp") so that the columns "x" and "y" become a single column "coordinates" with the format (0, 17) for x and y, how do you then call or manipulate data at a specific location?

The following:
e[e$coordinates==(0,17),]
Doesn't work.


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

Hi,
On Wed, Oct 12, 2011 at 3:37 PM, Bailey, Daniel <bailed at spu.edu> wrote:
That's kind of mangled, but what about:

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

(Depending on the form of your coordinates, you may also have to invoke FAQ 7.31.)

Sarah
--
Sarah Goslee
http://www.functionaldiversity.org
#
It's going to depend how the coordinates are stored within the data
frame. Do you perhaps know if they are factors or character strings?
(I'm not familiar with the package). If you don't know, type
str(NAMEOFYOUROBJECT) and we can help interpret the output.

Untested, I think this would actually work for both though:
e[as.character(e$coordinates)=="(0,17)",]

Michael
On Thu, Oct 13, 2011 at 2:05 PM, Bailey, Daniel <bailed at spu.edu> wrote:
#
Michael,
Thank you for the tips. The suggestion didn't work though. Here is the output of str(e):
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 168 obs. of  2 variables:
  .. ..$ catchcandata: num [1:168] 47.2 50.4 53.7 58 69.8 ...
  .. ..$ section     : Factor w/ 1 level "16 Sept F9": 1 NA NA NA NA NA NA NA NA NA ...
  ..@ coords.nrs : int [1:2] 1 2
  ..@ coords     : num [1:168, 1:2] 0 0 0 0 0 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] 0 0 48.8 17.1
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr NA 

-----Original Message-----
From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com] 
Sent: Thursday, October 13, 2011 11:13 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

It's going to depend how the coordinates are stored within the data frame. Do you perhaps know if they are factors or character strings?
(I'm not familiar with the package). If you don't know, type
str(NAMEOFYOUROBJECT) and we can help interpret the output.

Untested, I think this would actually work for both though:
e[as.character(e$coordinates)=="(0,17)",]

Michael
On Thu, Oct 13, 2011 at 2:05 PM, Bailey, Daniel <bailed at spu.edu> wrote:
#
Because SpatialPointsDataFrame is S4 object, you may try index by @
e at coords
or coordinates(e)

Weidong Gu
On Thu, Oct 13, 2011 at 2:18 PM, Bailey, Daniel <bailed at spu.edu> wrote:
#
Hi,
On Thu, Oct 13, 2011 at 2:05 PM, Bailey, Daniel <bailed at spu.edu> wrote:
They "don't become a single column" but rather a single matrix with
two columns, and (0, 17) isn't the correct way to specify a vector.
You can identify particular coordinates using the form I offered
earlier, and then use that to subset the data slot of your SGPF.

Using built-in data:

library(sp)
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,]

There ought to be a more elegant way to match coordinates (other than
the do.call() and paste() approach), but I'm not sure what it is.

Sarah
#
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:
#
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:
#
On Thu, Oct 13, 2011 at 2:44 PM, Bailey, Daniel <bailed at spu.edu> wrote:
You assign it a new value, just as for any assignment. Using the
example from my previous email:
x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    1     1
x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    5     1
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
#
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:
You assign it a new value, just as for any assignment. Using the example from my previous email:
x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    1     1
x      y part.a part.b dist soil ffreq
5 181100 333660      1      0    0    5     1
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
#
On 13-Oct-11 20:33, Sarah Goslee wrote:
Not sure if it is nicer, but another possibility is:
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
meuse.grid at data[which(duplicated(rbind(c(181100, 333660), 
coordinates(meuse.grid))))-1,]  = factor(c(1,2,3,4,1))

To avoid numerical problems, you can also find the data of the location 
closest to the point you are interested in:
meuse.grid at data[which.min(spDistsN1(meuse.grid, c(181100, 333660))),] = 
factor(c(1,1,1,1,1))

For questions about spatial data you can also use the mailing-list 
r-sig-geo.

Cheers,
Jon