Skip to content
Prev 201050 / 398502 Next

AKIMA: z values at a set coordinate

Hi Anonymous, (Maybe next time include your name)

There are data objects in R that are designed for spatial data, look at 
the sp package. Casting them into this format gives you an enormous 
increase in flexibility with analyzing spatial data. See the example 
below using your example:

library("akima")
library(sp)
data(akima)
akima.li <- interp(akima$x, akima$y, akima$z)
# Change to sp object
# Note that we swap the x and y column [1]
y = rep(akima.li$x, each = length(akima.li$y))
x = rep(akima.li$y, length(akima.li$x))
z = as.numeric(akima.li$z)
akima.sp = data.frame(x, y, z)
# sp-function, which columns are the coordinates
coordinates(akima.sp) = ~x+y
# Tell sp that it is a grid
gridded(akima.sp) = TRUE

# Plot and compare
image (akima.li)
spplot(akima.sp)

# Use overlay from sp to get the value
# at a specific location
pt = data.frame(x = 11.25, y = 6.5)
coordinates(pt) = ~x+y
val = akima.sp at data[overlay(akima.sp, pt),]
val
# [1] 19.14752

Learning to use sp-objects is really worthwhile. See the spatial Task 
view for more information, or check out the R-wiki [2]. With these kind 
of geographic questions you might want to use the r-sig-geo mailing list 
instead of R-help.

cheers,
Paul

[1] We do this because (from details section of Image):

Notice that ?image? interprets the ?z? matrix as a table of
?f(x[i], y[j])? values, so that the x axis corresponds to row
number and the y axis to column number, with column 1 at the
bottom, i.e. a 90 degree counter-clockwise rotation of the
conventional printed layout of a matrix.

[2] http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data
Rhelp wanted wrote: