-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathfactors.R
31 lines (29 loc) · 1.03 KB
/
factors.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#' cut methods for stars objects
#'
#' cut methods for stars objects
#' @name cut_stars
#' @param x see \link[base]{cut}
#' @param breaks see \link[base]{cut}
#' @param ... see \link[base]{cut}
#' @return an array or matrix with a \code{levels} attribute; see details
#' @details R's \code{factor} only works for vectors, not for arrays or matrices. This is a work-around (or hack?) to keep the factor levels generated by \code{cut} and use them in plots.
#' @export
cut.array = function(x, breaks, ...) {
d = dim(x)
x = cut(as.vector(x), breaks, ...)
structure(array(as.integer(x), dim = d), levels = levels(x))
}
#' @name cut_stars
#' @export
cut.matrix = cut.array
#' @name cut_stars
#' @export
#' @examples
#' tif = system.file("tif/L7_ETMs.tif", package = "stars")
#' x1 = read_stars(tif)
#' (x1_cut = cut(x1, breaks = c(0, 50, 100, Inf))) # shows factor in summary
#' plot(x1_cut[,,,c(3,6)]) # propagates through [ and plot
cut.stars = function(x, breaks, ...) {
ret = lapply(x, cut, breaks = breaks, ...)
st_stars(ret, st_dimensions(x))
}