I have a formula in R for example

I have a formula in R for example

y ~ x + z + xx + zz + tt + x:xx + x:zz + xx:z + zz:xx + xx:zz:tt

or even more complicated *(y~x*z*xx*zz*tt)*

Note that the names on the right-hand side of the formula are intentionally selected to be somehow similar to at least one other term.

The question is now how to remove the interaction terms that are related to a specific main effect. For example, if I remove the term x (main effect) I want to remove the interaction terms that also include x, here *x:xx.*

I have tried grepl() but it would remove any term that contains partially or fully the word. In my example it removes *x,xx,x:xx,xx:z,zz:xx,xx:zz:tt*

any ideas about a function to do it?

**Update**: What I have already tried:

f = y ~ x + z + xx + zz + tt + x:xx + x:zz + xx:z + zz:xx + xx:zz:tt modelTerms = attr(terms(f) , which = 'term.labels') modelTerms[!grepl(pattern = 'x', x = modelTerms)]

Learn the fundamentals of R markdown in this in-depth tutorial, or simply use it as a quick reference guide and cheatsheet for R markdown formatting.In this blog post, we’ll look at how to use R Markdown. By the end, you’ll have the skills you need to produce a document or presentation using R Mardown, from scratch!

Data types are kept easy. Data types of R are quite different when we compare with other programming languages. Here, we’ll outline the data types of R.

Make your R data analysis beautiful and your R programming workflow smoother with these tips, tricks, and shortcuts that'll turn you into an R markdown pro!

Data Visualization in R with ggplot2: A Beginner Tutorial. Learn to visualize your data using R and ggplot2 in this beginner-friendly tutorial that walks you through building a chart for data analysis.

Learn how to load a data set and clean it using R programming and tidyverse tools in this free beginner-level data analysis tutorial.