title: Conditional means based on other columns in R with dplyr
body: <p>Let's say I have the following data:</p>
  
  <pre><code>structure(list(political_spectrum = c(5L, 15L, 12L, 30L, 100L, 
  0L, 27L, 52L, 38L, 64L, 0L, 0L, 76L, 50L, 16L, 16L, 0L, 23L, 
  0L, 25L, 68L, 50L, 4L, 0L, 50L), politics_today = c("Independent", 
  "Strong Democrat", "Weak Democrat", "Weak Democrat", "Weak Republican", 
  "Strong Democrat", "Weak Democrat", "Weak Democrat", "Independent", 
  "Weak Democrat", "Strong Democrat", "Independent", "Weak Republican", 
  "Weak Democrat", "Weak Democrat", "Strong Democrat", "Strong Democrat", 
  "Strong Democrat", "Strong Democrat", "Strong Democrat", "Independent", 
  "Independent", "Strong Democrat", "Strong Democrat", "Independent"
  ), stranger_things_universe_mc = c("The Demagorgon", "", "", 
  "", "", "", "", "", "", "The Stranger Land", "The Demagorgon", 
  "The Upside Down", "", "", "", "", "", "The Upside Down", "The Shadowland", 
  "", "", "", "", "", "The Shadowland"), stranger_things_universe_answer = c("The Upside Down", 
  "", "", "", "", "", "", "", "", "The Upside Down", "The Upside Down", 
  "The Upside Down", "", "", "", "", "", "The Upside Down", "The Upside Down", 
  "", "", "", "", "", "The Upside Down"), stranger_things_universe_confidence = c(32L, 
  NA, NA, NA, NA, NA, NA, NA, NA, 67L, 94L, 89L, NA, NA, NA, NA, 
  NA, 51L, 10L, NA, NA, NA, NA, NA, 0L), stranger_things_universe_importance = c("Don't care at all", 
  "", "", "", "", "", "", "", "", "Care somewhat strongly", "Care a little", 
  "Care somewhat strongly", "", "", "", "", "", "Care somewhat", 
  "Don't care at all", "", "", "", "", "", "Don't care at all"), 
      tupac_mc = c("", "Biggie Smalls", "", "", "", "", "", "Biggie Smalls", 
      "Biggie Smalls", "", "", "Biggie Smalls", "", "", "", "", 
      "", "", "Biggie Smalls", "", "", "Ice Cube", "", "", ""), 
      tupac_answer = c("", "Biggie Smalls", "", "", "", "", "", 
      "Biggie Smalls", "Biggie Smalls", "", "", "Biggie Smalls", 
      "", "", "", "", "", "", "Biggie Smalls", "", "", "Biggie Smalls", 
      "", "", ""), tupac_confidence = c(NA, 70L, NA, NA, NA, NA, 
      NA, 71L, 76L, NA, NA, 100L, NA, NA, NA, NA, NA, NA, 100L, 
      NA, NA, 32L, NA, NA, NA), tupac_importance = c("", "Don't care at all", 
      "", "", "", "", "", "Care somewhat", "Don't care at all", 
      "", "", "Care strongly", "", "", "", "", "", "", "Care a little", 
      "", "", "Don't care at all", "", "", ""), uber_ceo_mc = c("John Zimmer", 
      "", "", "", "", "Travis Kalanick", "", "", "", "Travis Kalanick", 
      "", "", "", "", "", "", "", "John Zimmer", "Travis Kalanick", 
      "Travis Kalanick", "", "", "", "", ""), uber_ceo_answer = c("Travis Kalanick", 
      "", "", "", "", "Travis Kalanick", "", "", "", "Travis Kalanick", 
      "", "", "", "", "", "", "", "Travis Kalanick", "Travis Kalanick", 
      "Travis Kalanick", "", "", "", "", ""), uber_ceo_confidence = c(0L, 
      NA, NA, NA, NA, 94L, NA, NA, NA, 69L, NA, NA, NA, NA, NA, 
      NA, NA, 5L, 13L, 17L, NA, NA, NA, NA, NA), uber_ceo_importance = c("Don't care at all", 
      "", "", "", "", "Care strongly", "", "", "", "Care somewhat", 
      "", "", "", "", "", "", "", "Don't care at all", "Don't care at all", 
      "Care somewhat", "", "", "", "", ""), black_panther_mc = c("", 
      "T'Chaka", "", "", "", "", "", "", "", "", "", "", "", "", 
      "", "", "", "", "", "", "", "", "T'Chaka", "", ""), black_panther_answer = c("", 
      "T'Challa", "", "", "", "", "", "", "", "", "", "", "", "", 
      "", "", "", "", "", "", "", "", "T'Challa", "", ""), black_panther_confidence = c(NA, 
      63L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
      NA, NA, NA, NA, NA, NA, NA, 34L, NA, NA), black_panther_importance = c("", 
      "Don't care at all", "", "", "", "", "", "", "", "", "", 
      "", "", "", "", "", "", "", "", "", "", "", "Care a little", 
      "", ""), the_office_mc = c("The Mindy Project", "", "", "", 
      "", "", "", "", "", "", "", "", "", "", "The Office", "", 
      "", "The Mindy Project", "", "", "", "", "The Office", "", 
      ""), the_office_answer = c("The Office", "", "", "", "", 
      "", "", "", "", "", "", "", "", "", "The Office", "", "", 
      "The Office", "", "", "", "", "The Office", "", ""), the_office_confidence = c(43L, 
      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, 
      NA, 11L, NA, NA, NA, NA, 100L, NA, NA), the_office_importance = c("Don't care at all", 
      "", "", "", "", "", "", "", "", "", "", "", "", "", "Don't care at all", 
      "", "", "Care a little", "", "", "", "", "Care a little", 
      "", ""), arms_manufacturing_company_mc = c("J. Brockton &amp; Sons", 
      "", "", "O.F. Mossberg &amp; Sons", "", "", "", "", "", "", "", 
      "", "J. Brockton &amp; Sons", "", "", "", "", "", "", "", "", 
      "", "", "", "J. Brockton &amp; Sons"), arms_manufacturing_company_answer = c("J. Brockton &amp; Sons", 
      "", "", "J. Brockton &amp; Sons", "", "", "", "", "", "", "", 
      "", "J. Brockton &amp; Sons", "", "", "", "", "", "", "", "", 
      "", "", "", "J. Brockton &amp; Sons"), arms_manufacturing_company_confidence = c(91L, 
      NA, NA, 24L, NA, NA, NA, NA, NA, NA, NA, NA, 37L, NA, NA, 
      NA, NA, NA, NA, NA, NA, NA, NA, NA, 100L), arms_manufacturing_company_importance = c("Don't care at all", 
      "", "", "Don't care at all", "", "", "", "", "", "", "", 
      "", "Don't care at all", "", "", "", "", "", "", "", "", 
      "", "", "", "Don't care at all")), class = c("data.table", 
  "data.frame"), row.names = c(NA, -25L))
  </code></pre>
  
  <p>I'm trying to do something like the following:</p>
  
  <pre><code>test %&gt;%
    gather(name, value, -c('political_spectrum', 'politics_today')) %&gt;%
    filter(value != "") %&gt;%
    mutate(question_id = sub("_[^_]+$", "", name)) %&gt;%
    mutate(confidence = grepl("_confidence", name)) %&gt;%
    group_by(politics_today, question_id) %&gt;%
    summarize(mean_confidence = mean(value[confidence == "TRUE"]))
  </code></pre>
  
  <p>in which I get the mean_confidence values for each political affiliation, but only for specific rows in the "value" column.  In order to run the mean only on "confidence" columns, I am trying to do a filter via <code>mean(value[confidence == "TRUE"])</code>, but am not sure the correct way to do this. </p>