如何用X、Y向量创建带条件过滤的公式列表/tibble供map函数使用
Absolutely! This is totally doable with rep(), conditional filtering, reformulate(), and purrr::map() to tie it all together. Let's walk through a step-by-step solution that gives you either a list of tibbles (each length 9) or a formula list ready for mapping.
First, let's start with your base vectors:
X <- c("A", "B", "C") Y <- c("X", "L", "Z")
Step 1: Build a conditional filtering & processing function
We'll create a function that takes each value from Y, filters X based on your rules, generates the repeated 9-length vector, and creates the formula with reformulate():
library(purrr) library(tibble) library(dplyr) process_y_element <- function(y_val) { # Filter X based on Y value filtered_x <- case_when( y_val == "L" ~ X[X != "C"], # Remove "C" when Y is "L" y_val == "Z" ~ X[X != "B"], # Remove "B" when Y is "Z" TRUE ~ X # Keep full X for Y = "X" ) # Generate 9-length repeated X (uses length.out to guarantee exact length) repeated_x <- rep(filtered_x, length.out = 9) # Create the formula: Y value as response, filtered X terms as predictors # We use unique(filtered_x) because formulas don't need repeated variable names formula <- reformulate(termlabels = unique(filtered_x), response = y_val) # Package everything into a tibble (easy to work with) tibble( y = rep(y_val, 9), x = repeated_x, formula = list(formula) # Store formula as a list column to avoid flattening ) }
Step 2: Generate your desired output
Now use map() to apply this function to every element in Y:
Option 1: List of tibbles (3 elements, each length 9)
result_list <- map(Y, process_y_element) # Check the first element (Y = "X") result_list[[1]] #> # A tibble: 9 × 3 #> y x formula #> <chr> <chr> <list> #> 1 X A <formula> #> 2 X B <formula> #> 3 X C <formula> #> 4 X A <formula> #> 5 X B <formula> #> 6 X C <formula> #> 7 X A <formula> #> 8 X B <formula> #> 9 X C <formula>
Option 2: Single combined tibble (length 27)
If you prefer one unified table, use map_dfr():
result_tibble <- map_dfr(Y, process_y_element)
Option 3: Just the formula list (ready for map operations)
If you only need the formulas without the tibble wrapper, simplify the code:
formula_list <- map(Y, function(y_val) { filtered_x <- case_when( y_val == "L" ~ X[X != "C"], y_val == "Z" ~ X[X != "B"], TRUE ~ X ) reformulate(unique(filtered_x), y_val) }) # View the formulas formula_list #> [[1]] #> X ~ A + B + C #> <environment: 0x0000022a1e8f7d58> #> #> [[2]] #> L ~ A + B #> <environment: 0x0000022a1e8f7d58> #> #> [[3]] #> Z ~ A + C #> <environment: 0x0000022a1e8f7d58>
Quick notes:
- Using
rep(filtered_x, length.out = 9)is cleaner than calculatingnmanually—it automatically repeats the filtered vector enough times and truncates/pads to hit exactly 9 elements. - If you really need repeated variable names in your formulas (uncommon for statistical modeling, but possible for other use cases), just swap
unique(filtered_x)withrepeated_xin thereformulate()call.
内容的提问来源于stack exchange,提问作者Geet
相关产品推荐
相关产品推荐

