# Subindex using logical expression in Stan

**URL:** <https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463>\
**Category:** General\
**Created:** [January 27, 2021, 1:22am UTC](https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463 "2021-01-27T01:22:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [January 27, 2021, 1:22am UTC](https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463/1 "2021-01-27T01:22:51Z")

</div>

Hi,

I am wondering whether Stan allows subindexing a vector using logical expression like R. For example, suppose I have:

```stan
data{
int<lower = 0> N;
int<lower = 0> K;
vector<lower=0> [N] y;
vector<lower=0> [N] index; //eg (1, 1,1,2,2,3...)
}

```

Are we allowed to use expression like the following to select certain ys according an logical expression criteria? If not, do we have alternative expressions available in Stan?

```stan
generated quantities{
vector<lower = 0> [K] z;
}
for (i in 1 : K){
z[i] = sum(y[index == i]);
}

```

Thx!

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [February 1, 2021, 9:36am UTC](https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463/2 "2021-02-01T09:36:24Z")

</div>

Unfortunately this is not allowed in Stan at this time. Marking this as solved but feel free to continue the discussion.

---

<div class="post-metadata">

**Author:** ![stan\_beginer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stan\_beginer](https://discourse.mc-stan.org/u/stan_beginer)\
**Post date:** [February 1, 2021, 4:19pm UTC](https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463/3 "2021-02-01T16:19:59Z")

</div>

Sure and I found that the discussion in this post [Dealing with data subsetting in Stan - #7 by seantalts](https://discourse.mc-stan.org/t/dealing-with-data-subsetting-in-stan/9842/7) has some user-defined function to implement such purpose. Will the future version of Stan add this logical subsetting tool?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [February 1, 2021, 4:43pm UTC](https://discourse.mc-stan.org/t/subindex-using-logical-expression-in-stan/20463/4 "2021-02-01T16:43:32Z")

</div>

The only thing I know is that I am not aware of any such current efforts.

But this is an open source project so who knows, maybe someone is working on this we just dont know it yet :)
