0

I am trying to sum all of my raster files (over 500) from a single folder in R, using the raster package, but am encountering an error message I cannot figure out how to solve. I am trying to use to use this package to use the raster calculator to sum these files.

I have a folder of raster images (.tif). I then run the following code in R.

library(rgdal)
library(raster)

allrasters <- stack(list.files('C:/Users/MyName/Path/to/My/Data/Folder', full.names=T)

raster_sum <- calc(allrasters, fun=sum,na.rm=T, ,filename='C:/Users/MyName/Path/to/Outputs/Folder/Raster_Sum_Output.tif')

which yields the following error message:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘calc’ for signature ‘"RasterStack", "numeric"’

What might be the cause for this, and how can I fix this problem so that I can get my intended output raster which is the sum of all the raster files in my folder? have not been able to find online how I might address this issue, since the the "RasterStack" and "numeric" components are too specific.

1 Answer 1

2

Have you got a numeric object called sum?

> sum=99
> raster_sum <- calc(allrasters, fun=sum,na.rm=TRUE)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘calc’ for signature ‘"RasterStack", "numeric"’

The error is saying you are calling calc with a RasterLayer and a numeric argument, so the second argument must be numeric, and not a function. Remove any sum numeric object, or do base::sum:

> raster_sum <- calc(allrasters, base::sum, na.rm=TRUE)
> 

(Oh and always spell TRUE out because that can break stuff too...)

6
  • Thank you, this is working so far now, your suggestion of replacing fun=sum with base::sum got the code to run. Though the code has been running for a good 45 minutes now, and I'm wondering if these seems reasonable for summing over 500 raster files, or if I should expect this to run much quicker. Commented Oct 4, 2021 at 15:39
  • All depends on the number of pixels. Try with a few subsets, time it, estimate the time for the whole set...
    – Spacedman
    Commented Oct 4, 2021 at 15:52
  • Good suggestion, I just checked the raster information in QGIS, and each raster file has Pixel Size: 100,-100, which did not seem like that much to me at first, but maybe I was wrong and created rasters with too high a resolution. Commented Oct 4, 2021 at 16:06
  • "Pixel size" isn't the issue, pixel count is. Read one stack into R and check the summary.
    – Spacedman
    Commented Oct 4, 2021 at 17:51
  • I tried a subset folder with just 4 of the raster files, and the code ran and completed in just a few seconds, and looking out the output raster file in QGIS, it looks just as I would intend. Though the message: Warning in .gd_SetProject(object, ...) : NOT UPDATED FOR PROJ >= 6 was produced, and I am not sure if this is an issue. I am not sure about the summary, but I simply ran allrasters and was returned: dimensions : 6418, 7821, 50195178, 4 (nrow, ncol, ncell, nlayers), but I don't think that says anything about the pixel count. Maybe the pixel count is too high then? How do I check? Commented Oct 9, 2021 at 21:10

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.