% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hex_summarize.R
\name{hex_summarize}
\alias{hex_summarize}
\title{Summarize Data by Hex Cell}
\usage{
hex_summarize(hex_data, ..., .fns = NULL, geometry = FALSE)
}
\arguments{
\item{hex_data}{A HexData object (from \code{\link[=hexify]{hexify()}}).}

\item{...}{Named summary expressions (tidyeval). Each expression is
evaluated per cell group with the group's columns available by name.
Examples: \code{mean_temp = mean(temperature)},
\code{n_species = length(unique(species))},
\code{max_elev = max(elevation, na.rm = TRUE)}. Provide either \code{...} or
\code{.fns}, not both.}

\item{.fns}{Optional named list of functions for formula-style aggregation.
Example: \code{.fns = list(mean_temp = ~mean(temperature))}. Provide either
\code{...} or \code{.fns}, not both.}

\item{geometry}{Logical. If \code{TRUE}, attach cell center points as an
sf geometry column (requires sf). Default \code{FALSE}.}
}
\value{
A data.frame with columns:
\describe{
\item{cell_id}{Unique cell identifier}
\item{cell_cen_lon, cell_cen_lat}{Cell center coordinates}
\item{cell_area_km2}{Cell area in km^2}
\item{n_points}{Number of data points in this cell}
\item{...}{User-defined summary columns}
}
If \code{geometry = TRUE}, returns an sf object with POINT geometry.
}
\description{
Aggregates data within each hexagonal cell, similar to
\code{dplyr::group_by(cell_id) |> summarize(...)}. Returns a data.frame
with one row per unique cell, including cell center coordinates and area.
}
\details{
The function works entirely in R (no C++ needed). It groups by \code{cell_id}
and evaluates the summary expressions within each group.

If no summary expressions are provided, returns cell counts only.
}
\examples{
\donttest{
df <- data.frame(
  lon = runif(100, -10, 10),
  lat = runif(100, 40, 55),
  temperature = rnorm(100, 15, 5),
  species = sample(letters[1:5], 100, replace = TRUE)
)
hd <- hexify(df, lon = "lon", lat = "lat", area_km2 = 500)

# Count points per cell
hex_summarize(hd)

# Custom summaries
hex_summarize(hd, mean_temp = mean(temperature),
                  n_species = length(unique(species)))
}
}
\seealso{
\code{\link[=hexify]{hexify()}} for creating HexData objects,
\code{\link[=get_neighbors]{get_neighbors()}} for finding neighboring cells
}
