rp_summarize: Summarize data using pandas

View source: R/verbs_filter.R

rp_summarizeR Documentation

Summarize data using pandas

Description

Aggregates a data frame by one or more groups, applying summary functions. It translates R's dplyr::summarise syntax into a pandas ⁠.groupby().agg()⁠ command.

Usage

rp_summarize(.data, ..., .by = NULL, table_name = NULL, return.as = "result")

Arguments

.data

An R data.frame or tibble.

...

Named summary expressions (e.g., avg_price = mean(price)). Supports mean, median, sd, var, min, max, sum, and n().

.by

A bare column name or c(col1, col2) to group by.

table_name

An optional character string. If provided, the generated Python code will replace the internal dataframe name with this string (e.g., "diamonds.query(...)"). This is useful for seeing the exact, copy-pasteable Python code. Defaults to NULL (uses "df").

return.as

What to return: "result", "code", or "all".

Value

A data.frame with the summarized and grouped data.

Examples


if (reticulate::py_available(initialize = TRUE) &&
    reticulate::py_module_available("pandas")) {
  
  # Summarize by one group
  rp_summarize(ggplot2::diamonds, 
               avg_price = mean(price), 
               .by = cut)
  
  # Summarize by multiple groups and multiple functions
  rp_summarize(ggplot2::diamonds, 
               avg_price = mean(price), 
               count = n(),
               .by = c(cut, color))
}


rPandas documentation built on April 29, 2026, 1:07 a.m.