| db_bin | R Documentation |
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.
db_bin(var, bins = 30, binwidth = NULL)
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 |
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.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.