-1

I have the following code

  attrWeights = list(time    = exp(b_tt)/(exp(b_tt)+exp(b_tc)+exp(b_hw)+exp(b_ch)),
                     cost    = exp(b_tc)/(exp(b_tt)+exp(b_tc)+exp(b_hw)+exp(b_ch)),
                     headway = exp(b_hw)/(exp(b_tt)+exp(b_tc)+exp(b_hw)+exp(b_ch)),
                     changes = exp(b_ch)/(exp(b_tt)+exp(b_tc)+exp(b_hw)+exp(b_ch)))

It's pretty cumbersome to type in the denominator many times. Is there a way to simplify, such that

time = exp(b)/ c, where c is the summation of all the exponent terms?

thanks.

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
  • 1
    Your equations are inconsistent: you first list `a` over the sums of exponentiated `b` and `c`, but when you define `y` (as the sum of ..), you then divide `a` by `b`, not by `y`. Am I missing something? (This is before AnilGoyal's edit that assumed you should have `y` in the denominator ... perhaps yours was just a typo?) – r2evans May 29 '21 at 16:41
  • @r2evans, I am assuming he means `x = a/y` – AnilGoyal May 29 '21 at 16:41
  • 2
    I was thinking the same, but ... the question now has code that resolves the question, suggesting that I'm missing something else or the edit to the code resolves the question. – r2evans May 29 '21 at 16:42
  • 2
    Just calculate it once with assignment to a meaningful name like `denom`. – IRTFM May 29 '21 at 16:53
  • Thanks for the answers so far. The b_x terms to be estimated, thus the denominator are strings, to be fed in to another function, rather than numerical values at this point. – rocky_raccoon May 29 '21 at 17:11
  • 2
    That last comment is very confusing to me. Please make this question a bit more reproducible, ensuring that we can try all of your code and apply it with some expected output. The gold-standard for sample data is with `dput(x)`, where `x` is a representative sample of data; it doesn't need to be all-rows, all-columns, just something *representative*. I suggest you read https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info, then come back and add some more material to your question. Thanks! – r2evans May 29 '21 at 18:08

1 Answers1

0

Its hard sometimes to ask a question the right way, especially when one is new to R.

To answer my own question, we can do something like this:

denom <- c("(exp(b_tt)+exp(b_tc)+exp(b_hw)+exp(b_ch)")

time <- paste0("exp(b_tt)/(", denom,")")

Adding it back to the list should be trivial.

Thanks you for your help!