2
votes
require(raster)

## Function to aggregate
fun.patch <- function(x) {
  if (max(table(x)) >= 0.9 * length(x)) {
    return(as.vector(which.max(table(x))))
  }
  else
    return(NA)
}

r.lc <- raster(nrows = 100, ncols = 100)
r.lc[] <- 1:6
aggregate(r.lc, fact = c(5,5), fun.patch)

Error in FUN(newX[, i], ...) : unused argument(s) (na.rm = TRUE)

1
Look at ?raster::aggregate. The function you pass should accept or ignore a na.rm argument.mnel

1 Answers

5
votes

From ?raster::aggregate - The function you pass should accept or ignore a na.rm argument

To ignore, include ... in the function arguments

fun.patch <- function(x,...) {
  if (max(table(x)) >= 0.9 * length(x)) {
  return(as.vector(which.max(table(x))))
  }
else
  return(NA)
}

r.lc <- raster(nrows = 100, ncols = 100)
r.lc[] <- sample(1:6, 100^2, replace = T)
aggregate(r.lc, fact = c(5,5), fun.patch)