You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
aggregate() only works with functions that return a single number (e.g., sum, mean, modal, min, max). It would be very handy if there is also an option to get the frequency of each value in the aggregated cell (as with function "table"), particularly for rasters with "classes type" values.
To achieve that, I am currently doing the following (which is not very efficient). Maybe there already is a better way to do it?
library(terra)
r <- rast(nrow = 100, ncol = 100)
values(r) <- sample(1:8, 1e4, replace = T)
# it would be great if this works:
# ra <- aggregate(r, fact=4, fun = "table", na.rm = TRUE)
# right now I am doing:
ra <- vector(mode = "list", length = 8)
ra[[1]] <- aggregate(r, fact=4, fun = function(x) sum(x == 1, na.rm = TRUE))
ra[[2]] <- aggregate(r, fact=4, fun = function(x) sum(x == 2, na.rm = TRUE))
ra[[3]] <- aggregate(r, fact=4, fun = function(x) sum(x == 3, na.rm = TRUE))
ra[[4]] <- aggregate(r, fact=4, fun = function(x) sum(x == 4, na.rm = TRUE))
ra[[5]] <- aggregate(r, fact=4, fun = function(x) sum(x == 5, na.rm = TRUE))
ra[[6]] <- aggregate(r, fact=4, fun = function(x) sum(x == 6, na.rm = TRUE))
ra[[7]] <- aggregate(r, fact=4, fun = function(x) sum(x == 7, na.rm = TRUE))
ra[[8]] <- aggregate(r, fact=4, fun = function(x) sum(x == 8, na.rm = TRUE))
ra <- do.call(c, ra)
The text was updated successfully, but these errors were encountered:
Thanks!
I didn't realize that you can use a function that returns multiple values in aggregate. The function documentation states the opposite.
I notice that aggregate(r, fun = function(x) range(x)) also works but not aggregate(r, fun = function(x) unique(x)).
So, it works as long as it returns the same number of values, right?
Should this be clarified in the aggregate help file?
aggregate()
only works with functions that return a single number (e.g., sum, mean, modal, min, max). It would be very handy if there is also an option to get the frequency of each value in the aggregated cell (as with function "table"), particularly for rasters with "classes type" values.To achieve that, I am currently doing the following (which is not very efficient). Maybe there already is a better way to do it?
The text was updated successfully, but these errors were encountered: