Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a set of geocoded, pixel-aligned rasters which I would like to use in a model in R. I would like to produce a table of data that can be imported to R, in which there are as many columns as rasters, and as many rows as points at which data is extracted from the rasters. Say I have 3 rasters: r1, r2, r3. I would end up with a table that has r1, r2, r3 as the column headers. The rows would be point1, point2, point3 - and there would be as many rows as points in the layer used to define which points to extract data from. Forgive the bad table:

------r1---r2---r3

point1-1----4---2

point2-4----3---1

How can I do this? I've tried some Arc tools but they have failed or hung. I can use QGIS, Arc or Idrisi.

Is the Raster package for R a good approach?

share|improve this question
    
Would you rather simply import a raster into R and then do your raster analysis? So I better understand your goal, could you please clarify "...as many columns as rasters, and as many rows as pixels"? –  Aaron Jun 27 '12 at 12:17
    
I should have used the word 'point' rather than 'pixel'. Say I have 5 rasters: r1, r2, r3 etc I would end up with a table that has r1, r2, r3, r4, r5 as the column headers. The rows would be point1, point2, point3 - and there would be as many rows as points in the layer used to define which points to extract data from. I originally tried to draw the table to exemplify what I mean, but couldn't get the formatting right... –  gisol Jun 27 '12 at 13:11

2 Answers 2

up vote 5 down vote accepted

I think what you are after is:

library(raster)
library(rgdal)

s <- stack('r1.tif', 'r2.tif', 'r3.tif')
d <- values(s)

Avoid exporting grids to ascii, that only slows things down, and you loose information on the coordinate reference system.

share|improve this answer
    
Great, thanks - this produces the same results as the ASCII route given by whuber, but cuts out all the conversions. –  gisol Jun 29 '12 at 13:10
    
+1 Good, clean solution. Welcome to our site, Robert! –  whuber Jul 2 '12 at 11:39

Yes, use Raster. Here is an example using five coincident raster objects r.a ... r.e. In practice, you would read these from (say) ASCII exports of the grids:

data <- vapply(list(r.a,r.b,r.c,r.d,r.e), "[", rep(0.0, ncol(r.a)*nrow(r.a)))

The purpose of the "0.0" is to indicate you want an array of floats. If the data are integral, use an integer for this value instead.

share|improve this answer
    
Thanks for your answer. How would I go from my geoTIFF rasters to objects r.a, r.b loaded in R? Reading the Raster description‌​, I thought rasterStack sounded promising. –  gisol Jun 27 '12 at 15:01
    
Several solutions for reading Arc* grids are given at gis.stackexchange.com/questions/16720. I found this by searching the site for the "R" and "Raster" tags: you may find the other hits useful, too. –  whuber Jun 27 '12 at 15:04
    
Thanks, I've just finished reading all that - it was lazy of me to ask before searching myself! Other useful things in the 'related' column –  gisol Jun 27 '12 at 15:20
    
No problem--please just remember to vote up the useful answers (and the questions that prompted them, too)! –  whuber Jun 27 '12 at 15:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.