title: colSums if another column is true (for stacked barplot)
body: <p>I have:</p>
  
  <pre><code>$ Facebook       : int  0 0 0 0 0 0 0 1 1 0 ...
  $ YouTube        : int  0 0 0 0 0 0 0 0 0 0 ...
  ...
  $ Subscribed     : int  0 0 0 1 0 0 0 0 1 0 ...
  </code></pre>
  
  <p>If I use <code>sourceTotals = colSums(sources[c(2:13)])</code>, I'll generate a data frame with totals for each service.</p>
  
  <p>How can I generate something along the lines of:</p>
  
  <pre><code>Service      Subscribed NotSubscribed Total
  Facebook     50         50            100
  YouTube      10         235           245
  </code></pre>
  
  <p>(I will then try to generate a stacked barplot based on the new data frame)</p>
  
  <p>I tried things such as <code>sourceTotals = data.frame(colSums(ifelse((sources[c(2:13)]+sources[15])&gt;2, 1, 0)), colSums(ifelse((sources[c(2:13)]+sources[15])&gt;2, 1, 0)), colSums(sources[c(2:13)]))</code> but that deosn't seem to be it.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Solution based on Karolis' answer:</p>
  
  <pre><code>myData = sources[c(2:14)]
  
  sL = rep("none", nrow(myData))
  for(i in 1:ncol(myData)-1) {
      sL[as.logical(myData[,i])] &lt;- colnames(myData)[i]
  }
  
  sM = addmargins(table(sL, Subscribed=as.logical(myData$Subscribed)), 2)
  
  library(reshape2)
  sM1 = melt(sM[,c(1:2)])
  
  colnames(sM1) = c("Source", "Subscribed", "Users")
  
  ggplot(sM1, aes(x=Source, y=Users, fill=Subscribed)) + geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
  </code></pre>