title: Classifying zones in an x, y axis
body: <p>I have a dataset containing frame-by-frame x and y points of animals tracked from a video.</p>
  <p>It looks like this:</p>
  <pre><code>date       frame position     x     y
     &lt;date&gt;     &lt;dbl&gt; &lt;fct&gt;    &lt;dbl&gt; &lt;dbl&gt;
   1 2019-08-26     1 1        138.   67  
   2 2019-08-26     1 2        166    89.5
   3 2019-08-26     1 3        147   127  
   4 2019-08-26     1 4        113   130. 
   5 2019-08-26     1 5         91    98  
   6 2019-08-26     2 1        138.   67  
   7 2019-08-26     2 2        166    89.5
   8 2019-08-26     2 3        147   127  
   9 2019-08-26     2 4        112.  130. 
  10 2019-08-26     2 5         90.5  98  
  11 2019-08-26     3 1        138.   67  
  12 2019-08-26     3 2        166    89.5
  13 2019-08-26     3 3        146.  127  
  14 2019-08-26     3 4        112   130. 
  15 2019-08-26     3 5         91.2  98.2
  16 2019-08-26     4 1        138.   67  
  17 2019-08-26     4 2        166    89.5
  18 2019-08-26     4 3        147.  128. 
  19 2019-08-26     4 4        111.  129. 
  20 2019-08-26     4 5         91.2  97.2
  </code></pre>
  <p>From this, my goal is to classify the points into discrete observations by creating a new column called &quot;activity&quot;. The idea is to classify the points if they are within an imaginary polygon/area and must be within both the x and y argument and not only either one.</p>
  <p>For example, I want any points within 100 and 110 in y and 110 and 140 in x &quot;zone&quot; to be classified as &quot;interaction&quot;. Points outside the &quot;zone&quot; will be classified as something else.</p>
  <p>I have tried this:</p>
  <pre><code>full_data %&gt;% 
    mutate(activity = ifelse(y %in% c(100:110) &amp; x %in% c(110:140), &quot;interaction&quot;,  
                             ifelse(y &lt;= 30 &amp; y &gt;= 160 | x &lt;= 60 &amp; x &gt;= 190, &quot;outside&quot;, &quot;none&quot; ))) 
  </code></pre>
  <p>However, this does not seem to create the desired result as seen in this image:</p>
  <p><a href="https://i.stack.imgur.com/4qqHc.png" rel="nofollow noreferrer">See image here</a></p>
  <p>I initially thought that a nested ifelse statement with multiple arguments will be ok. Any suggestions on how to go about this problem would be appreciated. Thanks!</p>
  <p>EDIT 1:
  Added a subset of the full dataset. This looks long for a minimally reproducible example but it replicates the problem I have been having.</p>
  <pre><code>full_data_sub = structure(list(date = structure(c(18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134, 
                                                    18134, 18134, 18134, 18134, 18134, 18134, 18134, 18134), class = &quot;Date&quot;), 
                                 frame = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
                                           15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
                                           30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
                                           45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
                                           60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 
                                           75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 
                                           90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 
                                           104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 
                                           116, 117, 118, 119, 120), position = c(4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
                                                                                  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), 
                                 x = c(113, 112.5, 112, 111.4, 112.1, 112.1, 112.1, 112.1, 
                                       112.6, 112.75, 112.6, 113.1, 113.5, 112.25, 113, 112.9, 113, 
                                       113, 113, 113.1, 113, 113, 113, 112.5, 113.3, 112.4, 111.9, 
                                       112.2, 112.2, 112.8, 112.4, 113.2, 112.9, 113.5, 115, 115.1, 
                                       116.1, 116.6, 116.6, 117.6, 117.75, 117.9, 118.9, 120.1, 
                                       121.1, 122, 122, 123, 123, 123.5, 123.5, 123.1, 123.6, 123.6, 
                                       123.1, 121.6, 121.6, 121.1, 120.25, 119.75, 119.75, 119.6, 
                                       117.6, 116.6, 114.6, 114, 113.9, 113, 113, 113, 113.5, 112.5, 
                                       112, 111, 111, 111, 110.9, 109.4, 109.5, 109.5, 110, 109.9, 
                                       109.4, 111, 111, 112.5, 112, 113.5, 114.5, 115.5, 116, 115.5, 
                                       114, 113, 110, 110.5, 110.5, 111, 112, 115, 115.5, 117.5, 
                                       117.5, 118, 116.5, 115, 114.5, 115.5, 117, 118.5, 118, 118, 
                                       118, 117.5, 117.5, 117, 113.5, 110.5, 108, 107), y = c(129.5, 
                                                                                              129.5, 129.5, 128.55, 129.2, 129.7, 129.7, 129.7, 129.7, 
                                                                                              129.35, 129.2, 129.2, 129.5, 129.35, 129.5, 129.85, 129.5, 
                                                                                              129.5, 129.5, 129.7, 129.5, 130, 129.5, 129.5, 129.15, 128.85, 
                                                                                              128.35, 127.5, 126.5, 124.65, 123.85, 122.5, 120.35, 119.5, 
                                                                                              118, 117.7, 116.7, 116.2, 116.7, 117.2, 117.35, 117.55, 118.05, 
                                                                                              119.2, 119.7, 118.5, 117.5, 117, 116.5, 116.5, 116, 116.2, 
                                                                                              115.7, 115.2, 115.2, 114.2, 113.7, 113.7, 112.85, 111.85, 
                                                                                              111.35, 111.7, 112.2, 114.2, 113.7, 113.5, 114.35, 113.5, 
                                                                                              111.5, 110, 110, 110, 109.5, 109.5, 109, 107.5, 107.35, 105.85, 
                                                                                              105.5, 104, 102, 100.85, 99.85, 100, 100.5, 100, 99.5, 98.5, 
                                                                                              97, 97.5, 97.5, 98.5, 97.5, 95.5, 94.5, 94.5, 93, 90.5, 90, 
                                                                                              90.5, 91, 91, 91, 91.5, 89, 86.5, 85.5, 83, 81, 77.5, 75.5, 
                                                                                              75, 75, 74.5, 75, 75.5, 76, 75, 73, 71.5)), row.names = c(NA, 
                                                                                                                                                        -120L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>