I'm using the package velox to speed up raster extraction.

Simple question (and probably an easy one): what is the argument in R for fun=all values?

Context: In the raster package the function extract fun=NULL is to return all pixel values. In velox the extract requires an argument "fun=". What is the function argument that will return all the cell values like the fun=NULL of the extract in the raster package?

Bonus points if you have a function that will take the mean while ignoring NA values (which is what I'm trying to do anyhow). Something along the lines of

meanvalue <- raster::extract(testraster, testpoly, fun=mean if x !=NA)

Id then use it on a velox raster (should work the same) like this rather than the above code:

meanvalue <- velox.testraster$extract(testpoly, fun=mean if x !=NA)

Fun=mean doesn't work because some of my polygons have NA values for pixels since they are not covered by the raster.

  • values() for all values. Watch out the return types is matrix for brick/stack
    – mdsumner
    Commented Apr 9, 2017 at 21:23
  • in the velox code fun=values, fun=values(), fun=val, gives me the error Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘values’ for signature ‘"missing"’. I guess a better question would be how to get every output of the extraction, missing or values. Seems the issue may not be with values but the fact it's trying to return values that don't exist. Commented Apr 9, 2017 at 21:39
  • Sorry I wasn't paying enough attention, will try to explore properly and see
    – mdsumner
    Commented Apr 10, 2017 at 0:37

1 Answer 1


Got it:

fun <- function(x){mean(x, na.rm=TRUE)})

for velox:

sample.list <- sampleVeloxraster$extract(sampleshp, fun=function(x){mean(x, na.rm=TRUE)})

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.