在RStudio中生成字母仅出现一次的组合:基于给定数据框的需求
Got it, let's break this down step by step. You need to create every possible combination of the letters A through E where each letter only appears once in a single combination. We'll also cover how to tie these combinations back to the prices from your single, double, and triple data frames if needed.
Step 1: Define your element set
First, let's formalize the letters we're working with:
# Define the full set of elements elements <- c("A", "B", "C", "D", "E")
Step 2: Generate all unique combinations
We'll use R's built-in combn() function to generate combinations of every possible size (1 to 5 elements). We'll wrap this in lapply() to handle all sizes at once, then flatten the result into a single list:
# Generate all 1-element, 2-element, ..., 5-element unique combinations all_combinations <- lapply(1:length(elements), function(k) { combn(elements, k, simplify = FALSE) }) %>% unlist(recursive = FALSE)
This gives you a list where each item is a unique combination (e.g., ["A"], ["A", "B"], ["A", "B", "C"], up to ["A", "B", "C", "D", "E"]).
Step 3: Convert to a data frame (optional)
If you prefer a tabular format, convert the list to a data frame. We'll add columns for the combination (as a readable string) and the number of elements in each combination:
# Convert combinations to a data frame comb_df <- data.frame( Combination = sapply(all_combinations, paste, collapse = ", "), Size = sapply(all_combinations, length), stringsAsFactors = FALSE )
The resulting comb_df will look like this (abbreviated):
Combination Size 1 A 1 2 B 1 3 C 1 ... 9 A, B 2 10 A, C 2 ... 31 A, B, C, D, E 5
Step 4: Match prices from your existing data frames (optional)
To link these combinations to the prices in your single, double, and triple data frames, we'll first format each data frame's combinations into a consistent key (sorted, comma-separated strings) then merge the prices into our comb_df:
# Load dplyr for data manipulation (install if needed: install.packages("dplyr")) library(dplyr) # Format single data frame into a named price vector single_prices <- setNames(single$Price, single$Mat) # Format double data frame: sort Mat1/Mat2 to create consistent keys double_prices <- double %>% mutate(Combination = sapply(1:nrow(.), function(i) { paste(sort(c(.$Mat1[i], .$Mat2[i])), collapse = ", ") })) %>% select(Combination, Price) %>% tibble::deframe() # Format triple data frame: sort Mat1/Mat2/Mat3 for consistent keys triple_prices <- triple %>% mutate(Combination = sapply(1:nrow(.), function(i) { paste(sort(c(.$Mat1[i], .$Mat2[i], .$Mat3[i])), collapse = ", ") })) %>% select(Combination, Price) %>% tibble::deframe() # Combine all price vectors into one all_prices <- c(single_prices, double_prices, triple_prices) # Add prices to our combination data frame (NA for combinations not in original data) comb_df$Price <- all_prices[comb_df$Combination]
Now comb_df includes the price for every combination that exists in your original data frames, and NA for any combinations you didn't have (like 4-element or 5-element groups).
内容的提问来源于stack exchange,提问作者Bort54

