weight_by | R Documentation |
This is a "brute force" weighting procedure. Each row of the dataset is replicated "case weight" times. If 'weight' is not integer it will be rounded to the nearest integer. So cases with weight less than 0.5 will be removed from the dataset. Such weighting is used in the several statistical procedures in the SPSS Statistic, e. g. for the Spearman correlation coefficient or GLM.
weight_by(data, weight = NULL)
data |
data.frame, data.table or matrix. Dataset which will be weighted. |
weight |
unquoted column name of weights in 'data' or vector of weights. If it is NULL 'data' will be returned unchanged. |
'data' with each row replicated according to case weight.
data(state) # US states
# convert matrix to data.table
states = data.table(state.x77, keep.rownames = "State")
# create weighted dataset
states_weighted = states %>%
let(
# calculate 'weight' variable.
weight = Population/100
) %>%
weight_by(weight)
# Each row in the weighted dataset is represented proportionally to the population of the state
nrow(states) # unweigthed number of cases
nrow(states_weighted) # number of cases in the weighted dataset
str(states_weighted)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.