# Is there build-in ifelse() function for vectors?

**URL:** <https://discourse.mc-stan.org/t/is-there-build-in-ifelse-function-for-vectors/18941>\
**Category:** Modeling\
**Tags:** specification\
**Created:** [October 27, 2020, 6:23pm UTC](https://discourse.mc-stan.org/t/is-there-build-in-ifelse-function-for-vectors/18941 "2020-10-27T18:23:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobinz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tobinz/32/9669_2.png) [@tobinz](https://discourse.mc-stan.org/u/tobinz)\
**Post date:** [October 27, 2020, 6:23pm UTC](https://discourse.mc-stan.org/t/is-there-build-in-ifelse-function-for-vectors/18941/1 "2020-10-27T18:23:27Z")

</div>

In R, if I apply `ifelse( u > v, 1, 0)` for two vectors `u` and `v`, it returns a vector containing 1 and 0 with the same length as `u` and `v`.

In Stan, seems to me that the conditional operator ` condition ? 1:0` can only be used for real variables? So is there an equivalent function in Stan for vectors?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [October 27, 2020, 7:24pm UTC](https://discourse.mc-stan.org/t/is-there-build-in-ifelse-function-for-vectors/18941/2 "2020-10-27T19:24:18Z")

</div>

No

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [October 27, 2020, 7:25pm UTC](https://discourse.mc-stan.org/t/is-there-build-in-ifelse-function-for-vectors/18941/3 "2020-10-27T19:25:49Z")

</div>

Currently I don’t think this exists but I think it’s a good idea. For now you could write a user defined function that does this via a loop (the loop won’t be too slow), that way you could still call it like a function in your Stan program.
