0

I have 2 rasters, ones shows aspect and another shows date of snow thaw. I want to extract the date of snow melt based on values from the aspect raster. For example I want all the snow thaw dates from south facing slopes. I have already identifies north (1), east(2), south(3), west(4). I have tried to extract the values:

snowThawSouth <- extract(aspect==3, snowThawDate)

However I just got the following error:

Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘extract’ for signature ‘"RasterLayer", "RasterLayer"’
In addition: Warning message:
closing unused connection 3 (/var/folders/4_/9jsy7tfj2yq0s_czc2gc6tvh0000gn/T/R_raster_USER//2015-07-17_152719_21839_88030.gri)

I just want to be able to extract all the values for each aspect, into a list, in order to compare the snow thaw dates based on the aspect of the cell.

2
  • do you just want the values or do you want it as separate rasters?
    – aaryno
    Jul 17 '15 at 17:01
  • I just need a list of the values
    – Thoron
    Jul 17 '15 at 17:04
1

extract pulls raster values out based on an intersection with a vector. That could be helpful if you wanted to sample raster values given something like a points shapefile. In this case, however, you want a to identify the cells based on value, and then get the data directly from the raster. This may need to be modified if performance in reading the data is an issue:

# Get the indexes which represent the different aspects. 
# These correspond to the cell indexes for north, east, etc.
idxNorth <- which(getValues(aspect)==1)
idxEast <- which(getValues(aspect)==2)
idxSouth <- which(getValues(aspect)==3)
idxWest <- which(getValues(aspect)==4)

We can use those indexes to extract the data directly into a vector:

snowThawNorth <- getValues(snowThawDate)[idxNorth]
snowThawEast <- getValues(snowThawDate)[idxEast]
snowThawEast <- getValues(snowThawDate)[idxSouth]
snowThawWest <- getValues(snowThawDate)[idxWest]

As @JeffEvans notes, these can be nested for brevity:

snowThawNorth <- getValues(snowThawDate)[which(getValues(aspect)==1)]
snowThawEast <- getValues(snowThawDate)[which(getValues(aspect)==2)]
snowThawEast <- getValues(snowThawDate)[which(getValues(aspect)==3)]
snowThawEast <- getValues(snowThawDate)[which(getValues(aspect)==4)]
2
  • 1
    You can nest even further by just applying the which index directly: snowThawNorth <- getValues(snowThawDate)[which(getValues(aspect)==1)] Jul 17 '15 at 17:54
  • Thanks worked a treat. Apologies for not marking the question answered until now.
    – Thoron
    Aug 6 '15 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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