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


regex r

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Getting Started with R Markdown — Guide and Cheatsheet

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 In R

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.

R Markdown Tips, Tricks, and Shortcuts

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

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.

Tutorial: Loading and Cleaning Data with R and the tidyverse

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