corrSubset: Extract Variable Subsets from a CorrCombo Object

View source: R/corrSubset.R

corrSubsetR Documentation

Extract Variable Subsets from a CorrCombo Object

Description

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.

Usage

corrSubset(res, df, which = "best", keepExtra = FALSE)

Arguments

res

A CorrCombo object returned by corrSelect or MatSelect.

df

A data frame or matrix. Must contain all variables listed in res@var_names. Columns not in res@var_names are ignored unless keepExtra = TRUE.

which

Subsets to extract. One of:

  • "best" (default) or 1: 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 in df not in res@var_names (e.g., factors, characters) are retained. Defaults to FALSE.

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).

Note

A warning is issued if any rows contain missing values in the selected variables.

See Also

corrSelect, MatSelect, CorrCombo

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)


corrselect documentation built on Aug. 23, 2026, 1:06 a.m.