weight2rows: Expand a weighted data frame to an equivalent unweighted

View source: R/weight2rows.R

weight2rowsR Documentation

Expand a weighted data frame to an equivalent unweighted

Description

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.

Usage

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")
weight2rows(DT, "y", rows.out = 5)


hutils documentation built on April 13, 2022, 5:23 p.m.