title: How do I apply conditions on a particular group element and find permutations from another group in the same table?
body: <p>I have the following data.frame</p>
  
  <pre><code>  Category Product Status
  1        A     qwe     In
  2        A     rty     In
  3        A     ewq    Out
  4        B     dfs     In
  5        B     sgf     In
  6        C     mnb    Out
  7        C     ves    Out
  8        C     klm    Out
  9        C     nbc    Out
  </code></pre>
  
  <p>My goal is to create a column <strong>Flag</strong> per group from the <strong>Category</strong> where the levels are <strong><em>OnlyIn, OnlyOut</em></strong> and <strong><em>BothInOut</em></strong>, corresponding to the values from the <strong>Status</strong> Column.</p>
  
  <p>As a part of it I calculated Counts on In and Out per group using the below code:</p>
  
  <pre><code>Data &lt;- Data %&gt;% 
    group_by(Category,Status) %&gt;% 
    dplyr::mutate(InCounts = length(Status[Status == "in"]),
                  OutCounts = length(Status[Status == "out"]))
  </code></pre>
  
  <p>And I got the below result :</p>
  
  <pre><code>  Category Product Status CountIn CountOut
  1        A     qwe     In       2        0
  2        A     rty     In       2        0
  3        A     ewq    Out       0        1
  4        B     dfs     In       2        0
  5        B     sgf     In       2        0
  6        C     mnb    Out       0        4
  7        C     ves    Out       0        4
  8        C     klm    Out       0        4
  9        C     nbc    Out       0        4
  </code></pre>
  
  <p>Now, I'm not sure on how to leverage this information to create the new column Flag by counting the total In and Out per Category and add an appropriate value. </p>
  
  <p>Example, if there are both in and out as status for a Category, then the Flag should read "BothInOut"</p>
  
  <p>Sample Output :</p>
  
  <pre><code>  Category Product Status CountIn CountOut      Flag
  1        A     qwe     In       2        0 BothInOut
  2        A     rty     In       2        0 BothInOut
  3        A     ewq    Out       0        1 BothInOut
  4        B     dfs     In       2        0    OnlyIn
  5        B     sgf     In       2        0    OnlyIn
  6        C     mnb    Out       0        4   OnlyOut
  7        C     ves    Out       0        4   OnlyOut
  8        C     klm    Out       0        4   OnlyOut
  9        C     nbc    Out       0        4   OnlyOut
  </code></pre>
  
  <hr>
  
  <p>Reproducible Input for Data</p>
  
  <pre><code>structure(list(Category = c("A", "A", "A", "B", "B", "C", "C", 
  "C", "C"), Product = c("qwe", "rty", "ewq", "dfs", "sgf", "mnb", 
  "ves", "klm", "nbc"), Status = c("In", "In", "Out", "In", "In", 
  "Out", "Out", "Out", "Out"), CountIn = c(2, 2, 0, 2, 2, 0, 0, 
  0, 0), CountOut = c(0, 0, 1, 0, 0, 4, 4, 4, 4), Flag = c("BothInOut", 
  "BothInOut", "BothInOut", "OnlyIn", "OnlyIn", "OnlyOut", "OnlyOut", 
  "OnlyOut", "OnlyOut")), .Names = c("Category", "Product", "Status", 
  "CountIn", "CountOut", "Flag"), row.names = c(NA, 9L), class = "data.frame")
  </code></pre>