db_compute_bins: Calculate histogram bins

View source: R/histogram.R

db_compute_binsR Documentation

Calculate histogram bins

Description

Uses dplyr operations to create histogram bins. Because of this approach, the calculations automatically run inside the database if 'data' has a database or sparklyr connection. The 'class()' of such tables in R are: tbl_sql, tbl_dbi, tbl_spark

Usage

db_compute_bins(data, x, bins = 30, binwidth = NULL)

Arguments

data

A table (tbl)

x

A continuous variable

bins

Number of bins. Defaults to 30.

binwidth

Fixed width for each bin, in the same units as the data. Overrides bins when specified

Value

An ungrouped data.frame with two columns: the bin values for the x variable and the count of observations in each bin.

See Also

db_bin,

Examples

## Not run: 
library(DBI)
library(dplyr)
con <- dbConnect(duckdb::duckdb(), ":memory:")
db_mtcars <- copy_to(con, mtcars, "mtcars")

# Returns record count for 30 bins in mpg
db_mtcars |>
  db_compute_bins(mpg)

# Returns record count for bins of size 10
db_mtcars |>
  db_compute_bins(mpg, binwidth = 10)

dbDisconnect(con)

## End(Not run)

dbplot documentation built on March 13, 2026, 5:06 p.m.