sjstats (version 0.17.5)

weight: Weight a variable

Description

These functions weight the variable x by a specific vector of weights.

Usage

weight(x, weights, digits = 0)

weight2(x, weights)

Arguments

x

(Unweighted) variable.

weights

Vector with same length as x, which contains weight factors. Each value of x has a specific assigned weight in weights.

digits

Numeric value indicating the number of decimal places to be used for rounding the weighted values. By default, this value is 0, i.e. the returned values are integer values.

Value

The weighted x.

Details

weight2() sums up all weights values of the associated categories of x, whereas weight() uses a xtabs formula to weight cases. Thus, weight() may return a vector of different length than x.

Examples

Run this code
# NOT RUN {
v <- sample(1:4, 20, TRUE)
table(v)
w <- abs(rnorm(20))
table(weight(v, w))
table(weight2(v, w))

set.seed(1)
x <- sample(letters[1:5], size = 20, replace = TRUE)
w <- runif(n = 20)

table(x)
table(weight(x, w))

# }

Run the code above in your browser using DataCamp Workspace