6

I have 2 rasters. One raster (bio1) contains temperature information across a continent. The other (sp) contains, for each cell, a probability of occurrence for a species. I would like to calculate the average temperature across the continent, where the average is weighted according to the probability values for each cell.

Any ideas?

sp<-raster(ncol=12,nrow=6)
sp[]<-c(0.3659248,0.3251995,0.3263240,0.4835116,0.6494138,0.5220696,0.4151727,0.6915647,0.5147048,0.2470879,0.2054283,0.1863406,0.2673065,0.2962143,0.2796624,0.4613542,0.4592576,0.2794486,0.2651766,0.4326347,0.3203363,0.2654715,0.1993542,0.2030421,0.2529734,0.2539935,0.2607953,0.2750939,0.2666969,0.2316632,0.2347576,0.2307931,0.2251635,0.2262545,0.1979716,0.1856529,0.2378655,0.2322828,0.2469152,0.2390453,0.2385228,0.2264184,0.2275306,0.2265828,0.2102648,0.2297261,0.1984145,0.1980131,0.2277406,0.2315173,0.2225589,0.2391843,0.2375292,0.2271611,0.2050888,0.2188710,0.2208964,0.2017645,0.1957584,0.1824360,0.2086336,0.2104096,0.2153448,0.2204493,0.2174766,0.2268214,0.2246680,0.1954685,0.1959728,0.2130131,0.1927875,0.1941573)

bio1<-raster(ncol=12,nrow=6)
bio1[]<-c(257,264,262,234,207,225,238,194,220,262,267,267,259,266,266,236,236,262,265,234,252,258,267,267,268,267,265,265,266,267,267,267,267,266,267,267,267,267,267,267,266,267,267,267,266,264,267,267,267,267,267,267,267,267,267,267,267,267,267,267,266,267,267,267,267,267,267,267,267,267,267,267)

1 Answer 1

7

I am not sure that there is a "memory safe" way to do this but you can just call the raster values in the weighted.mean function in base.

weighted.mean(x=values(bio1), w=values(sp), na.rm=TRUE)
0

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.