db_bin: Bin formula

View source: R/dbbin.R

db_binR Documentation

Bin formula

Description

Uses the rlang package to build the formula needed to create the bins of a numeric variable in an unevaluated fashion. This way, the formula can be then passed inside a dplyr verb.

Usage

db_bin(var, bins = 30, binwidth = NULL)

Arguments

var

Variable name or formula

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 unevaluated expression (rlang quosure) that calculates bin membership for the specified variable. This expression is designed to be used within dplyr verbs using the '!!' operator.

Examples

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

# Important: Always name the field and
# prefix the function with `!!` (See Details)

# Uses the default 30 bins
db_mtcars |>
  group_by(x = !!db_bin(mpg)) |>
  count()

# Uses binwidth which overrides bins
db_mtcars |>
  group_by(x = !!db_bin(mpg, binwidth = 10)) |>
  count()

dbDisconnect(con)

## End(Not run)

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