title: applying simple function to data
body: <p>Hi I try to apply simple function to data to create <code>sub_id</code> for groups.   </p>
  
  <pre><code>test = data.frame(gr=gl(2,4), id =rep(c("Good","bad","ugly","dirty"),2),
                          count=c(175,1,13,11, 10,165,10,2))  
  
  
  &gt; test
    gr    id count
  1  1  Good   175
  2  1   bad     1
  3  1  ugly    13
  4  1 dirty    11
  5  2  Good    10
  6  2   bad   165
  7  2  ugly    10
  8  2 dirty     2
  </code></pre>
  
  <p>the condition for <code>sub_id</code> is like this</p>
  
  <p>if group number is equal to minimum <code>count</code> with when <code>id==bad</code>, those groups sub_id is <code>red flag</code> else (other groups that do not satisfies this condition) <code>green flag</code>. </p>
  
  <p>So I wrote this function </p>
  
  <pre><code>  sub_id &lt;- function(gr,count,id){
      if (gr==min(count)&amp;id=="bad"){
        "red flag"
  
      }
      else
      "green flag"  
    }
  </code></pre>
  
  <p>and tried </p>
  
  <pre><code>library(dplyr)
  
    test%&gt;%
      group_by(gr)%&gt;%
      mutate(color=sub_id(gr,count,id))
  </code></pre>
  
  <p>gives me </p>
  
  <pre><code># A tibble: 8 x 4
  # Groups:   gr [2]
        gr     id count      color
    &lt;fctr&gt; &lt;fctr&gt; &lt;dbl&gt;      &lt;chr&gt;
  1      1   Good   175 green flag
  2      1    bad     1 green flag
  3      1   ugly    13 green flag
  4      1  dirty    11 green flag
  5      2   Good    10 green flag
  6      2    bad   165 green flag
  7      2   ugly    10 green flag
  8      2  dirty     2 green flag
  Warning messages:
  1: In if (gr == min(count) &amp; id == "Bad") { :
    the condition has length &gt; 1 and only the first element will be used
  2: In if (gr == min(count) &amp; id == "Bad") { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>the expected output</p>
  
  <pre><code>      gr     id count      color
    &lt;fctr&gt; &lt;fctr&gt; &lt;dbl&gt;      &lt;chr&gt;
  1      1   Good   175   red flag
  2      1    bad     1   red flag
  3      1   ugly    13   red flag
  4      1  dirty    11   red flag
  5      2   Good    10 green flag
  6      2    bad   165 green flag
  7      2   ugly    10 green flag
  8      2  dirty     2 green flag
  </code></pre>