Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:47:00+00:00 2026-06-13T08:47:00+00:00

Everyone else’s functions seem to take formula objects and then do dark magic to

  • 0

Everyone else’s functions seem to take formula objects and then do dark magic to them somewhere deep inside and I’m jealous.

I’m writing a function that fits multiple models. Parts of the formulas for these models remain the same and part change from one model to the next. The clumsy way would be to have the user input the formula parts as character strings, do some character manipulation on them, and then use as.formula.

But before I go that route, I just want to make sure that I’m not overlooking some cleaner way of doing it that would allow the function to accept formulas in the standard R format (e.g. extracted from other formula-using objects).

I want something like…

> LHS <- y~1; RHS <- ~a+b; c(LHS,RHS);
y ~ a + b
> RHS2 <- ~c;
> c(LHS, RHS, RHS2);
y ~ a + b + c

or…

> LHS + RHS;
y ~ a + b
> LHS + RHS + RHS2;
y ~ a + b + c

…but unfortunately neither syntax works. Does anybody know if there is something that does? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:47:01+00:00Added an answer on June 13, 2026 at 8:47 am

    reformulate will do what you want.

    reformulate(termlabels = c('x','z'), response = 'y')
    ## y ~ x + z
    

    Or without an intercept

    reformulate(termlabels = c('x','z'), response = 'y', intercept = FALSE)
    ## y ~ x + z - 1
    

    Note that you cannot construct formulae with multiple reponses such as x+y ~z+b

    reformulate(termlabels = c('x','y'), response = c('z','b'))
    z ~ x + y
    

    To extract the terms from an existing formula (given your example)

    attr(terms(RHS), 'term.labels')
    ## [1] "a" "b"
    

    To get the response is slightly different, a simple approach (for a single variable response).

    as.character(LHS)[2]
    ## [1] 'y'
    
    
    combine_formula <- function(LHS, RHS){
      .terms <- lapply(RHS, terms)
      new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
      response <- as.character(LHS)[2]
    
      reformulate(new_terms, response)
    
    
    }
    
    
    combine_formula(LHS, list(RHS, RHS2))
    
    ## y ~ a + b + c
    ## <environment: 0x577fb908>
    

    I think it would be more sensible to specify the response as a character vector, something like

    combine_formula2 <- function(response, RHS, intercept = TRUE){
      .terms <- lapply(RHS, terms)
      new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
      response <- as.character(LHS)[2]
    
      reformulate(new_terms, response, intercept)
    
    
    }
    combine_formula2('y', list(RHS, RHS2))
    

    you could also define a + operator to work with formulae (update setting an new method for formula objects)

    `+.formula` <- function(e1,e2){
      .terms <- lapply(c(e1,e2), terms)
      reformulate(unique(unlist(lapply(.terms, attr, which = 'term.labels'))))
    }
    
    RHS + RHS2
    ## ~a + b + c
    

    You can also use update.formula using . judiciously

     update(~a+b, y ~ .)
     ##  y~a+b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot seem to get this. Everyone else examples or questions use different functions
While everyone else loves those rounded corners and asks how to make them, I
This may seem quiete easy to everyone else but I am struggling to comprehend
I seem to be having the opposite problem as everyone else, my Javascript isn't
EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH
I am attempting to create a custom android style xml. And while everyone else
Can I ignore files locally without polluting the global git config for everyone else?
I've seen this all over the place on stack overflow, but everyone else's solution
I've read dozens of posts on this topic, but everyone else is having horizontal
Sorry if this is obvious to everyone else but I am having a minor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.