在R语言中获取有序排列组合及筛选特定4元组合的方法
Hey there! Let's tackle your two R questions one by one:
combn() to Return 4-Element Combinations with 1 & No 3+ Consecutive Numbers First, let's break this down into two clear steps: generating combinations that include 1, then filtering out any that have three or more consecutive digits.
Step 1: Get all 4-element combinations containing 1
Since combn() returns combinations as columns in a matrix, we can first generate all possible 4-element combinations, then keep only the columns that include the number 1:
example <- c(1, 2, 3, 4, 5, 6) # Generate all 4-element combinations, filter to keep only those with 1 all_with_1 <- combn(example, 4)[, apply(combn(example, 4), 2, function(col) 1 %in% col)]
Step 2: Filter out combinations with 3+ consecutive numbers
We need a helper function to check if a sorted combination has three consecutive digits. Since combn() returns combinations in sorted order, we can check if there's any triplet where the third number is exactly 2 more than the first (e.g., 2,3,4 → 4 = 2 + 2):
# Helper function to detect 3+ consecutive numbers has_three_consecutive <- function(col) { # Check if any three elements are consecutive any(col[-c(1,2)] == col[-c(length(col)-1, length(col))] + 2) } # Filter the valid combinations valid_combinations <- all_with_1[, !apply(all_with_1, 2, has_three_consecutive)]
If you print valid_combinations, you'll get exactly the set you listed:
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 1 1 1 [2,] 2 2 2 3 3 3 [3,] 4 4 5 4 4 5 [4,] 5 6 6 5 6 6
When order matters, we're talking about permutations (for no repeated elements) or ordered sequences with replacement (if repeats are allowed). Here's how to handle both cases:
Case 1: Permutations (no repeated elements, order matters)
If you want all unique ordered sequences of k elements from your vector (no repeats), you can use the permutations() function from the gtools package (a popular, lightweight tool for combinatorics):
# Install and load gtools if you haven't already # install.packages("gtools") library(gtools) # Get all ordered 4-element permutations of `example` permutations <- permutations(n = length(example), k = 4, v = example)
Each row in the resulting matrix is a unique ordered permutation (e.g., 1,2,3,4 is treated as distinct from 2,1,3,4).
If you prefer a base R solution without external packages, use this recursive function:
get_permutations <- function(vec, k) { if (k == 1) return(matrix(vec, ncol = 1)) prev_perms <- get_permutations(vec, k - 1) # Bind each element to permutations that don't already include it do.call(rbind, lapply(vec, function(x) { cbind(x, prev_perms[!apply(prev_perms, 1, function(row) x %in% row), ]) })) } # Example usage base_r_perms <- get_permutations(example, 4)
Case 2: Ordered sequences with replacement (allow repeats)
If you want ordered sequences where elements can repeat (e.g., 1,1,2,3 is allowed), use expand.grid() in base R:
# Get all ordered 4-element sequences with replacement ordered_with_replacement <- expand.grid(rep(list(example), 4))
Each row here represents an ordered sequence, and duplicates are permitted (the column order corresponds to the sequence order).
内容的提问来源于stack exchange,提问作者min7b5_b9_b11_b13

