title: "map" function repeats on first match in tibble, not returning new value for each row
body: <p>Earlier, I asked how to create [global environment objects from a tibble <a href="https://stackoverflow.com/questions/69470394/iteratively-create-global-environment-objects-from-tibble">1</a>, which I have been able to do.</p>
  <p>Now, I want to take the objects generated from the tibble to use in a function similar to this:</p>
  <pre><code>positives = function(pos_tibble){
    data_tibble %&gt;%
    filter(str_detect(Animal_ID, &quot;[:digit:]{2,3}&quot;)) %&gt;%
    mutate(Positive = if_else(U_mL&gt; cutoff(x), TRUE, FALSE))
  }
  </code></pre>
  <p>where cutoff(x) is as follows:</p>
  <pre><code>cutoff &lt;- function(x){
    if_else(&quot;Species_ab&quot; == &quot;Mouse&quot; &amp; &quot;Protein&quot; == &quot;GP&quot;, return(Mouse_IgG_GP_cutoff), 
            (if_else(&quot;Species_ab&quot; == &quot;Mouse&quot; &amp; &quot;Protein&quot; == &quot;NP&quot;, return(Mouse_IgG_NP_cutoff),
                     (if_else(&quot;Species_ab&quot; == &quot;Rat&quot; &amp; &quot;Protein&quot; == &quot;GP&quot;, return(Mouse_IgG_GP_cutoff),
                              return(Rat_IgG_NP_cutoff))))))
  }
  </code></pre>
  <p>so that <code>pos_tibble</code> would look like this:</p>
  <pre><code># Groups:   Species_ab, Antibody, Protein, Positive [6]
    Animal_ID Species_ab Antibody Protein   U_mL Positive
    &lt;chr&gt;     &lt;chr&gt;      &lt;chr&gt;    &lt;chr&gt;    &lt;dbl&gt; &lt;lgl&gt;   
  1 35        Mouse      IgG      GP      128.   TRUE    
  2 39        Mouse      IgG      GP        4.75 FALSE   
  3 43        Mouse      IgG      GP       NA    FALSE   
  4 186       Mouse      IgG      NP        5.57 FALSE   
  5 187       Mouse      IgG      NP        8.25 FALSE   
  6 44        Rat        IgG      GP       NA    FALSE   
  7 45        Rat        IgG      GP        9.49 TRUE    
  8 322       Rat        IgG      NP       NA    FALSE   
  9 323       Rat        IgG      NP       NA    FALSE 
  </code></pre>
  <p>I have tried mapping cutoff() multiple different ways, and cannot get it to select based off the conditions. It only repeats the first value matched.</p>
  <pre class="lang-r prettyprint-override"><code>by(neg_tibble, seq_len(nrow(neg_tibble)), cutoff)   
  
  seq_len(nrow(neg_tibble)): 1
  [1] 28.20336
  ----------------------------------------------------------- 
  seq_len(nrow(neg_tibble)): 2
  [1] 28.20336
  ----------------------------------------------------------- 
  seq_len(nrow(neg_tibble)): 3
  [1] 28.20336
  ----------------------------------------------------------- 
  seq_len(nrow(neg_tibble)): 4
  [1] 28.20336
  
  map(seq_len(nrow(neg_tibble)), cutoff)  
  
  [[1]]
  [1] 28.20336
  
  [[2]]
  [1] 28.20336
  
  [[3]]
  [1] 28.20336
  
  [[4]]
  [1] 28.20336
  
  map_dbl(seq_len(nrow(neg_tibble)), cutoff)
  
  [1] 28.20336 28.20336 28.20336 28.20336
  
  cutoff(neg_tibble)
  
  [1] 28.20336 
  
  apply(neg_tibble,1,cutoff)
  
  [1] 28.20336 28.20336 28.20336 28.20336
  </code></pre>
  <p>The above were taken from various other questions <a href="https://stackoverflow.com/questions/1699046/for-each-row-in-an-r-dataframe">a</a>, <a href="https://stackoverflow.com/questions/1699046/for-each-row-in-an-r-dataframe/1699296#1699296">b</a>, <a href="https://stackoverflow.com/questions/5330146/apply-a-function-to-each-row-in-a-data-frame-in-r">c</a>, <a href="https://stackoverflow.com/questions/53105121/iterating-function-over-every-row-in-data-frame-in-r">d</a>
  At the end of the day, I think <code>map</code> is what I want, but I need it to go through all my options, not repeat the first one.</p>
  <hr />
  <p>Additionally, I would love any recommendations for online resources (so I can not post as many questions!!!). I have been using <a href="https://r4ds.had.co.nz/" rel="nofollow noreferrer">r4ds</a> and <a href="https://software-carpentry.org/lessons/" rel="nofollow noreferrer">software carpentry</a> which have been helping a lot, though I'd like something a bit more comprehensive. I'm in the last two-ish months of my PhD program and am really only now getting into R a little bit of python to compile and clean all of my data into one a single tidy file, which I can then quickly run basic stats on (but I need it totally cleaned to get to that point).</p>
  <p>Thank you!</p>
  <p><strong>EDIT</strong> dput of a small sample of <code>pos_tibble</code></p>
  <pre><code>    &gt; dput(pos_tibble[c(4:6, 300:301, 500:501, 700:701),])
  structure(list(Animal_ID = c(&quot;35&quot;, &quot;39&quot;, &quot;43&quot;, &quot;Blank_Use&quot;, &quot;2x Blank&quot;, 
  &quot;525&quot;, &quot;526&quot;, &quot;44&quot;, &quot;45&quot;), Species_ab = c(&quot;Mouse&quot;, &quot;Mouse&quot;, &quot;Mouse&quot;, 
  &quot;Mouse&quot;, &quot;Mouse&quot;, &quot;Mouse&quot;, &quot;Mouse&quot;, &quot;Rat&quot;, &quot;Rat&quot;), Antibody = c(&quot;IgG&quot;, 
  &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;, &quot;IgG&quot;), Protein = c(&quot;GP&quot;, 
  &quot;GP&quot;, &quot;GP&quot;, &quot;NP&quot;, &quot;NP&quot;, &quot;NP&quot;, &quot;NP&quot;, &quot;NP&quot;, &quot;NP&quot;), U_mL = c(128.424549479298, 
  4.75300605155077, NA, 11.2504537822992, 78.8606956731523, 39.7199412048613, 
  NA, NA, NA)), row.names = c(NA, -9L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
  &quot;data.frame&quot;))
  </code></pre>
  <p><sup>Created on 2021-10-08 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>