# Inconsistencies subtracting rasters in R [closed]

I am new in GIS.

I have two rasters Aand B, with A representing a "future" status and B a present status. I want to compute and visualiza the net increase in A with respect to B.

Approach 1: I compute the sumof all cells in each layer as follows:

A_sum <- cellStats(A,"sum", na.rm=T)
B_sum <- cellStats(B,"sum", na.rm=T)
A_sum - B_sum
16.37

Approach 2: I create a layer with the same idea: visualiza the result map of increase in A with respect to B:

ABDif <- overlay(A, B, fun=function(r1, r2){return(r1-r2)})
plot(ABDif)
cellStats(ABDif,"sum", na.rm=T)
29.81

How on Earth both approaches render different results?

What is the correct one?

How would you calculate the % increase in A respect to B?

• Is A always going to be larger than B? Jun 27 '17 at 22:42
• In theory every pixel in A should be larger than B or equal, so the differences in A-B shouldn't be negative as far as I know. Would that cause the differences in the approaches? If that's the case I should double check Jun 28 '17 at 7:33
• First, all you have to do to subtract two rasters is a - b. Second, it helps to read the function help. The cellStats function defaults to a sample of cells and not every cell. Jun 28 '17 at 9:54
• Don't waste your time if you are going to reply with such arrogance. Replies like this makes me think this community is not as kind and helpful with newbies as others Jun 28 '17 at 13:15
• The cellStats function help for raster v. 2.5-8 says "cellStats returns a single value, computed from the all the values of a layer", which unfortunately directly contradicts the Usage section, which does clearly show that parameter 'sample' defaults to TRUE. I'd missed that myself, and the docs should really be corrected. That is likely the root of the problem, try setting sample to FALSE and then compare your methods again. Jun 28 '17 at 13:58