# R/x3p_average.R In x3ptools: Tools for Working with 3D Surface Measurements

#### Documented in x3p_average

```#' Average an x3p object
#'
#' Calculate blockwise summary statistics on the surface matrix of an x3p.
#' If the x3p object has a mask, the mode of the mask value
#' @param x3p x3p object
#' @param b positive integer value, block size
#' @param f function aggregate function
#' @param ... parameters passed on to function f. Make sure to use na.rm = T as needed.
#' @export
#' @importFrom dplyr group_by summarize ungroup add_tally
#' @examples
#' small <- x3p_average(logo)
x3p_average <- function(x3p, b = 10, f = mean, ...) {
stopifnot("x3p" %in% class(x3p))
x <- y <- value <- NULL # pass R CMD CHECK

df <- x3p_to_df(x3p)
scale <- x3p_get_scale(x3p)
df\$x <- round(df\$x/scale,0)
df\$y <- round(df\$y/scale,0)
# round x and y to block size
df\$x <- df\$x %/% b
df\$y <- df\$y %/% b
#  browser()

df <- summarize(group_by(df, x, y),
value = f(value, ...),
)
#   browser()
#   annotations <- data.frame(
#     annotation = names(colors)
#     )
#   df <- left_join(
#       annotations,
#     )
# }

} else {
df <- summarize(group_by(df, x, y),
value = f(value, ...)
)
}

# scale x and y back
df\$x <- df\$x * scale * b
df\$y <- df\$y * scale * b
df_to_x3p(df)
}
```

## Try the x3ptools package in your browser

Any scripts or data that you put into this service are public.

x3ptools documentation built on May 29, 2024, 10:33 a.m.