I have two lists:

```
n <- 'winner'
p_list <- c('qualified', 'female', 'apple')
df_features <- c('female','qualified','admission','apple_B','apple_C','apple_D')

```

I want to generate a formula like so given p\_list and df\_features:

```
winner ~ apple_B + apple_C + apple_D + female + qualified

```

Basically I am given `p_list` and `n`. I want to create a formula with `n` being the outcome and `p_list` being the regressors. However if one of the elements in `p_list` is not in `df_features`, I want to alter that element to be replaced by anything with the same text before the underscore (\_) from `df_features`. So apple would be replaced by apple\_B + apple\_C + apple\_D. Hopefully this makes sense.

How can I do this in R (I prefer a solution if dplyr if possible).

I've tried this so far:

```
f <- as.formula(paste(n,"~",paste(p_list,collapse="+")))

```

But right now the solution is not accounting for `df_features` and the altering of the variable apple.

I'm also able to check if values in `p_list` are in `df_features` by `p_list %in% df_features`, but not sure how to use it right now.