[R-sig-Geo] Creating a data frame from a set of rasters that includes XY values as columns

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Feb 24 19:26:34 CET 2015



On 02/24/2015 06:19 PM, Damian Maddalena wrote:
> Hello.
> 
> I am trying to create a data frame from a series of rasters that
> includes a column for each raster and a column for the corresponding XY
> values. IE: I want each row to be the cell values at every XY location.
> 
> For example, if I pull down the worldclim bioclim data (below), I would
> like to unpack one or more rasters from the raster stack and assemble
> them as a data frame with X and Y as an attribute. IE: I want a data
> frame where each record is a cell, and the attributes are X,Y,Z1...Zx.
> 
>> library(raster)
>> d <- getData('worldclim', var='bio', res=10)
> 
> I have done this in GRASS with all data imported but I would like to try
> to do it in R in the most succinct way possible. I am getting tripped up
> on the structure of the raster stack.
> 
> Any hints on how to proceed would be appreciated. Should I just pull out
> the raster values, turn them into a vector, slap them into a data frame,
> then append the range for the XY? Would that line up correctly?
> 

Yes, or even simpler:

> as(as(d, "SpatialPointsDataFrame"), "data.frame")[1:5,]
  bio1 bio2 bio3  bio4 bio5 bio6 bio7 bio8 bio9 bio10 bio11 bio12
1 -174   67   17 11862   37 -356  393  -31 -307    -7  -307   144
2 -174   67   17 11870   37 -355  392  -30 -219    -7  -307   143
3 -172   68   17 11872   39 -354  393  -29 -217    -5  -305   136
4 -173   68   17 11887   39 -354  393  -29 -217    -5  -306   136
5 -173   68   17 11877   39 -354  393  -29 -217    -6  -306   136
  bio13 bio14 bio15 bio16 bio17 bio18 bio19         x        y
1    22     7    38    59    24    50    24 -37.91667 83.58333
2    22     7    42    59    23    50    24 -37.75000 83.58333
3    22     6    42    57    22    49    23 -36.91667 83.58333
4    22     6    42    57    22    49    23 -36.75000 83.58333
5    22     6    42    57    22    49    23 -36.58333 83.58333

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi),  University of Münster,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150224/3dc3facc/attachment.bin>


More information about the R-sig-Geo mailing list