# Stan equivalent of R's %in% function

**URL:** <https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849>\
**Category:** RStan\
**Created:** [April 12, 2018, 6:37pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849 "2018-04-12T18:37:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [April 12, 2018, 6:37pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/1 "2018-04-12T18:37:52Z")

</div>

Hi all -

I had a need to create a version of R’s `%in%` function for Stan, so I decided to post the code here in case anyone finds it useful. Essentially checks if an integer matches a list of integers, and is useful for determining if an element index is in a pre-specified list of indices. It is only vectorized one way (the list of possible index matches), instead of 2-way as R’s is.

[test\_R\_in.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/5/5e40097755080cd4c4eda4ee1f629a255b7d0f4f.stan) (790 Bytes)  
[R\_in.R](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/13ccb915f3b338b7c11b5e3642b86527235050ef.R) (326 Bytes)

Here’s the Stan code, and demonstration R code is in the attached files:

```
functions {
  // function that is comparable to R's %in% function
  // pos is the value to test for matches
  // array pos_var is the 1-dimensional array of possible matches
  // returns pos_match=1 if pos matches at least one element of pos_var and pos_match=0 otherwise
  // example code: 
  // if(r_in(3,{1,2,3,4})) will evaluate as TRUE
  int r_in(int pos,int[] pos_var) {
    int pos_match;
    int all_matches[size(pos_var)];
    
    for (p in 1:(size(pos_var))) {
      all_matches[p] = (pos_var[p]==pos);
    }
    
    if(sum(all_matches)>0) {
      pos_match = 1;
      return pos_match;
    } else {
      pos_match = 0;
      return pos_match;
    }
    
  }
}

```

---

<div class="post-metadata">

**Author:** ![aaronjg](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aaronjg/32/3768_2.png) [@aaronjg](https://discourse.mc-stan.org/u/aaronjg)\
**Post date:** [April 12, 2018, 9:06pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/2 "2018-04-12T21:06:36Z")

</div>

That’s great! Would it be possible to use the `size()` function and avoid the third argument?

---

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [April 13, 2018, 1:14am UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/3 "2018-04-13T01:14:03Z")

</div>

Yes! Thanks for the tip. Code updated.

---

<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:** [April 13, 2018, 2:00am UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/4 "2018-04-13T02:00:49Z")

</div>

Nice! @Bob_Carpenter I’m curious, is this also how you would code this up?

Somewhat related: A while ago I started planning and fiddling with an R package of tested user defined functions in the Stan language, which I hope to resume soonishly (will be on GitHub, contributions welcome). Functions would be exposed to R so users can try them out in R before using them in Stan programs, and they would be able to be added to Stan programs easily. This seems like a good candidate for one of the early entries.

---

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [April 13, 2018, 12:16pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/5 "2018-04-13T12:16:30Z")

</div>

Sure thing Jonah. I think that kind of package would be useful if we can find good categories for functions so that people can find what they are looking for, i.e., data-munging vs. statistical vs. arithmetic functions.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [April 13, 2018, 12:52pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/6 "2018-04-13T12:52:26Z")

</div>

A package as you suggest would be really useful. I have a huge `utils.stan` file by now which make my life so much simpler when coding up some data munging or whatever in Stan.

What I am saying is that I would try to contribute to this.

---

<div class="post-metadata">

**Author:** ![dpastoor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dpastoor/32/2179_2.png) [@dpastoor](https://discourse.mc-stan.org/u/dpastoor)\
**Post date:** [April 15, 2018, 6:20pm UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/7 "2018-04-15T18:20:12Z")

</div>

> [@saudiwin](#):
>
> functions {  
> // function that is comparable to R’s %in% function  
> // pos is the value to test for matches  
> // array pos\_var is the 1-dimensional array of possible matches  
> // returns pos\_match=1 if pos matches at least one element of pos\_var and pos\_match=0 otherwise  
> // example code:  
> // if(r\_in(3,{1,2,3,4})) will evaluate as TRUE  
> int r\_in(int pos,int pos\_var) {  
> int pos\_match;  
> int all\_matches[size(pos\_var)];
> 
> ```
> for (p in 1:(size(pos_var))) {
> all_matches[p] = (pos_var[p]==pos);
> }
> 
> if(sum(all_matches)&gt;0) {
> pos_match = 1;
> return pos_match;
> } else {
> pos_match = 0;
> return pos_match;
> }
> 
> ```
> 
> }  
> }

I’m not sure how ‘hot’ the paths you’d use this ever would be, but here are some considerations to optimize the code considerably

```no-highlight
functions {
   // if(r_in(3,{1,2,3,4})) will evaluate as 1
  int r_in(int pos,int[] pos_var) {
   
    for (p in 1:(size(pos_var))) {
       if (pos_var[p]==pos) {
       // can return immediately, as soon as find a match
          return 1;
       } 
    }
    return 0;
  }
}

```

Two things that are changed

1. definitely no need to sum everything, as that essentially makes you traverse the vector twice, on top of doing more work (summing).

2. can exit as soon as find a match, no need to keep scanning, as we know at least one is present, if you get through the entire loop without an exit, that means no matches so return 0. If you do need to iterate over the entire thing (say return the number of times was in the vector), then store the count elsewhere and increment it every time a match, rather than doing a sum of a whole bunch of 0’s.

In general, the ‘tricks’ such as using sum for presence (within R itself) are artifacts of R’s inherent slowness of explicit loops, so explicitly looping and returning early will actually be slower than just doing a vectorized sum. In C++, this is not the case, so you can take advantage of not needing to think always in terms of contorting activities to be vectorized.

Finally, I haven’t been keeping up with stan these days so I’m not sure if its valid or not, but might want to consider changing the function signature as the return value being a boolean, rather than 0/1.

Cheers

---

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [April 16, 2018, 1:24am UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/8 "2018-04-16T01:24:09Z")

</div>

Thanks much! Faster code is always appreciated. I’ll update the post.

---

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [April 16, 2018, 1:26am UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/9 "2018-04-16T01:26:05Z")

</div>

Also re: return value, the Stan manual says boolean functions return 0 or 1, so just going off of that.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [April 17, 2018, 1:24am UTC](https://discourse.mc-stan.org/t/stan-equivalent-of-rs-in-function/3849/10 "2018-04-17T01:24:54Z")

</div>

> [@jonah](#):
>
> is this also how you would code this up?

I’d have coded it the way @dpastoor did for exactly the reasons he gave.

> [@dpastoor](#):
>
> consider changing the function signature as the return value being a boolean, rather than 0/1

That’s all we have for boolean in Stan to date. I’d like to add a proper boolean type. It’d be a subtype of `int` the way `int` is a subtype of `real` so as not to break backward compatibility.
