Extracts one or more variable subsets from a CorrCombo object as data frames.
Typically used after corrSelect or MatSelect to obtain filtered
versions of the original dataset containing only low‐correlation variable combinations.
Arguments
- res
A
CorrComboobject returned bycorrSelectorMatSelect.- df
A data frame or matrix. Must contain all variables listed in
res@var_names. Columns not inres@var_namesare ignored unlesskeepExtra = TRUE.- which
Subsets to extract. One of:
"best"(default) or1: the top‐ranked subset.A single integer (e.g.
2): the nth ranked subset.A vector of integers (e.g.
1:3): multiple subsets."all": all available subsets.
Subsets are ranked by decreasing size, then increasing average correlation. Subsets tying on both keep the order the search enumerated them in, which is itself determined by the input, so the ranking is reproducible across platforms. Exact ties are common – for instance, when every pair exceeds the threshold, every subset is a single variable with an average correlation of 0.
- keepExtra
Logical. If
TRUE, columns indfnot inres@var_names(e.g., factors, characters) are retained. Defaults toFALSE.
Value
A data frame if a single subset is extracted, or a list of data frames if multiple subsets are extracted. Each data frame contains the selected variables (and optionally extras).
Examples
# Simulate input data
set.seed(123)
df <- as.data.frame(matrix(rnorm(100), nrow = 10))
colnames(df) <- paste0("V", 1:10)
# Compute correlation matrix
cmat <- cor(df)
# Select subsets using MatSelect (cmat is already a correlation matrix)
res <- MatSelect(cmat, threshold = 0.5)
# Extract the best subset (default)
corrSubset(res, df)
# Extract the second-best subset
corrSubset(res, df, which = 2)
# Extract the first three subsets
corrSubset(res, df, which = 1:3)
# Extract all subsets
corrSubset(res, df, which = "all")
# Extract best subset and retain additional numeric column
df$CopyV1 <- df$V1
corrSubset(res, df, which = 1, keepExtra = TRUE)