Present since v1.0.0. Argument rows.out available since v1.3.0; rows.out < 1 supported since v 1.4.0. Argument discard_weight.var available since v1.3.0.

weight2rows(DT, weight.var, rows.out = NULL,
  discard_weight.var = FALSE)

Arguments

DT

A data.table. Will be converted to one if possible.

weight.var

Variable in DT to be used as weights.

rows.out

If not NULL (the default) specifies the number of rows in the result; otherwise the number of rows will be sum(DT[[weight.var]]). (Due to rounding, this figures are inexact.)

Since v1.4.0, if 0 < rows.out < 1 then taken to be a sample of the unweighted table. (So rows.out = 0.1 would give a 10% sample.)

discard_weight.var

If FALSE, the default, weight.var in DT will be 1 for each row in the result or a new weight if rows.out is given. Otherwise, TRUE drops the column entirely.

Value

DT but with the number of rows expanded to sum(DT[[weight.var]]) to reflect the weighting.

Examples

library(data.table) DT <- data.table(x = 1:5, y = c(1, 1, 1, 1, 2)) weight2rows(DT, "y")
#> x y #> 1: 1 1 #> 2: 2 1 #> 3: 3 1 #> 4: 4 1 #> 5: 5 1 #> 6: 5 1
weight2rows(DT, "y", rows.out = 5)
#> x y #> 1: 1 0.8333333 #> 2: 2 0.8333333 #> 3: 3 0.8333333 #> 4: 4 0.8333333 #> 5: 5 0.8333333 #> 6: 5 0.8333333