I would like to use something like dplyr's cut_number
to split a column into buckets with approximately the same number of observations, where my dataset is in a compact form where each row has a weight (number of observations).
Example data frame:
df <- data.frame(
x=c(18,17,18.5,20,20.5,24,24.4,18.3,31,34,39,20,19,34,23),
weight=c(1,10,3,6,19,20,34,66,2,3,1,6,9,15,21)
)
If there were one observation of x per row, I would simply use df$bucket <- cut_number(df$x,3)
to segment x
into 3 buckets with approximately the same number of observations. But how do I take into account the fact that each row is weighted with some number of observations? I'd like to avoid splitting each row into weight
rows since the original dataframe already has millions of rows.
df <- data.frame(x=1:6, weight=c(1,1,1,1,4,1))
, do you draw the buckets as123|455|556
or123|45|6
(where|
denotes a bucket boundary)?