Learn R Programming

ClusterBootstrap (version 2.0.0)

clusterResample: Cluster Resampling

Description

Performs hierarchical (clustered or nested) resampling of a data frame across one or more grouping variables. Each level of grouping can be resampled with or without replacement.

Usage

clusterResample(df, clusters, replace)

Value

A resampled data.table with the same column structure as df, potentially with repeated or dropped rows depending on replace.

Arguments

df

A data frame or data table. The original dataset to be resampled.

clusters

A character vector of variable names that define the nested structure of the data. The order should be from highest (outermost) to lowest (innermost) level.

replace

A logical vector, of the same length as clusters, indicating whether to sample with replacement at each level.

Author

Mathijs Deen

Details

This function supports arbitrary nesting depth, and preserves the original hierarchical structure during resampling. At each level, sampling is done conditionally within the grouping structure defined by the higher levels.

See Also

clusterBootstrap that uses the current function.

Examples

Run this code
if (FALSE) {
set.seed(123)
df <- expand.grid(
  school = paste0("S", 1:5),
  class  = paste0("C", 1:5),
  student = paste0("P", 1:5)
)
df$score <- rnorm(nrow(df))

resampled <- clusterResample(df, clusters = c("school", "class", "student"),
                              replace = c(TRUE, TRUE, FALSE))
}

Run the code above in your browser using DataLab