Search code examples
rgeospatialraster

Why does raster::extract return several values when using a buffer?


I am trying to extract values from a raster based on points. When I run this code

library(raster)

raster::extract(my_raster, my_points, method='simple')

I get one raster value for each point. However, when I try and do the same with the buffer

raster::extract(my_raster, my_points, method='simple', buffer=250)

I get several values per each point. I read the documentation of the function in detail, but I can not understand why do I get several raster values when I draw a buffer around my points, or how to avoid this.

Thank you.


Solution

  • This is condensed from ?extract

    library(raster)
    r <- raster(ncol=36, nrow=18, vals=1:(18*36))
    xy <- cbind(-50, seq(-80, 80, by=20))
    
    ## no function 
    extract(r, xy[1:3,], buffer=1000000)
    #[[1]]
    # [1] 586 587 588 589 590 591 592 593 620 621 622 623 624 625 626 627 628 629 630 631
    #[[2]]
    #[1] 517 518 552 553 554 555
    #[[3]]
    #[1] 445 446 481 482
    
    ## mean works
    extract(r, xy[1:3,], buffer=1000000, fun=mean)
    #[1] 611.1 541.5 463.5
    
    ## but "mean" fails
    extract(r, xy[1:3,], buffer=1000000, fun="mean")
    #Error in fun(x) : could not find function "fun"