title: R: Sum variables based on some condition
body: <p>Given a table, I am trying to learn how to use R for summing variables based on when a certain condition is met (based on other variables in the same table).</p>
  <p>Using the dplyr library, (I created some data) and then summed the data by groups :</p>
  <pre><code>#load library
  library(dplyr)
  
  #create data
  data &lt;- data.frame(
      
      &quot;col_a&quot; = c(&quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;),
      &quot;col_b&quot; = c(&quot;123&quot;, &quot;124&quot;, &quot;125&quot;, &quot;126&quot;, &quot;127&quot;, &quot;128&quot;, &quot;129&quot;, &quot;130&quot;),
  &quot;col_c&quot; = c(&quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;),
  &quot;col_d&quot; = c(&quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;blue&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;green&quot;, &quot;green&quot;),
  &quot;day_a&quot; = c(&quot;2001-01-01&quot;, &quot;2000-01-05&quot;, &quot;2000-01-01&quot;, &quot;2010-12-20&quot;, &quot;2010-12-20&quot;, &quot;2020-05-05&quot;, &quot;2020-05-05&quot;, &quot;2020-05-28&quot;),
  &quot;day_b&quot; = c(&quot;2001-01-10&quot;, &quot;2000-01-10&quot;, &quot;2000-01-01&quot;, &quot;2010-12-25&quot;, &quot;2010-12-22&quot;, &quot;2020-05-15&quot;, &quot;2020-05-20&quot;, &quot;2020-05-30&quot;)
  
  )
  
  #format variable types
  
  data$col_a = as.factor(data$col_a)
  data$col_b = as.factor(data$col_b)
  data$col_c = as.factor(data$col_c)
  
  #format date variables
  data$day_a = as.factor(data$day_a)
  data$day_b = as.factor(data$day_b)
  
  data$day_1 = as.Date(as.character(data$day_a))
  data$day_2 = as.Date(as.character(data$day_b))
  
  #create new variable based on difference between date variables
  data$diff = data$day_2 - data$day_1
  data$diff = as.numeric(data$diff)
  
  #create file that sums days based on groups of &quot;col_a, col_c, col_d&quot;
  file = data%&gt;%
      group_by(col_a, col_c, col_d) %&gt;% 
      dplyr::summarize(Total = sum(diff, na.rm=TRUE), Count = n()) 
  
  file = as.data.frame(file)
  </code></pre>
  <p>Now, for groups of &quot;col_a, col_c, col_d&quot;, I want to sum the &quot;diff&quot; variable based on another condition.</p>
  <p>For example, for the group &quot;aaa, 2015, green&quot;, I want to only sum the &quot;unique days&quot; - i.e. the days that overlap. (2020-05-05, 2020-05-15), ( 2020-05-05, 2020-05-20), (2020-05-28 ,2020-05-30)</p>
  <p>For this group, I want the value of the &quot;total&quot; variable = 15 + 2 = 17 ... instead of &quot;27&quot;.</p>
  <p>This is because the dates(2020-05-05, 2020-05-15) is completely within the dates ( 2020-05-05, 2020-05-20). I only want to sum the &quot;unique&quot; date periods.</p>
  <p>I am trying to get something in the end that looks like this:</p>
  <pre><code>final_result &lt;- data.frame ( col_a = c(&quot;aaa&quot;, &quot;aaa&quot;, &quot;aaa&quot;),
                               col_c = c(&quot;2015&quot;, &quot;2015&quot;, &quot;2015&quot;), 
                               col_d = c(&quot;blue&quot;, &quot;green&quot;, &quot;red&quot;),
                               total = c(&quot;5&quot;,&quot;17&quot;,&quot;9&quot;),
  count = c(&quot;2&quot;, &quot;3&quot;, &quot;3&quot;)
                                   
      )
  </code></pre>
  <p>Can anyone please show me how to do this?</p>
  <p>Thanks</p>