Removing the interaction terms when the main effect is removed

Removing the interaction terms when the main effect is removed

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)]


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

R Programming For Beginners - R Language Tutorial - R Tutorial For Beginners

R Programming For Beginners - R Language Tutorial - R Tutorial For Beginners

How to Use Regex in Java

In this article, we will learn about Java Regex, and how to use Regex with examples in Java. Java regex is also known as Java Regular Expression.

From ‘R vs Python’ to ‘R and Python’

In this article, you'll learn to leverage the best of both ‘Python and R’ in a single project.