2

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?

2
  • 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
    Commented Jun 27, 2012 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
    Commented Jun 27, 2012 at 13:11

2 Answers 2

5

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.

1
  • Great, thanks - this produces the same results as the ASCII route given by whuber, but cuts out all the conversions.
    – gisol
    Commented Jun 29, 2012 at 13:10
3

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.

4
  • 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
    Commented Jun 27, 2012 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
    Commented Jun 27, 2012 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
    Commented Jun 27, 2012 at 15:20
  • No problem--please just remember to vote up the useful answers (and the questions that prompted them, too)!
    – whuber
    Commented Jun 27, 2012 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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