title: How to use summarise function in dplyr with multiple conditions?
body: <p>I am working with a time-series cross-country dataset covering the period from 2003 to 2018. Each entry in the database corresponds to a protest event, the number of participants, level of engagement of the security services, and level of participant violence. I have multiple observations per year per country. I want to create a new <code>df</code> that counts the number of protests for each country (<code>Count</code>), the average number of participants (<code>AvgParticipants</code>), the average security services engagement (<code>AvgSecurity</code>), and the average level of participant violence (<code>AvgPartViolence</code>). Here is the code I have written thus far:</p>
  <pre><code># Creating Yearly Protest Count Data
  # Load packages
  library(dplyr)
  
  # Set working directory 
  setwd(&quot;~/Desktop/Cooptation and Protest&quot;)
  
  # Load data
  dat &lt;- read.csv(&quot;reports.csv&quot;)
  
  # Subset to relevant variables
  dat &lt;- dat %&gt;%
    select(cowcode, event_date, side, scope, part_violence, sec_engagement,
           numparticipants)
  
  # Convert event_date to only year
  dat$event_date &lt;-  as.Date(dat$event_date)
  dat$year &lt;- as.numeric(format(dat$event_date,'%Y'))
  
  my_summary_data &lt;- dat %&gt;%
    group_by(year, cowcode) %&gt;%
    summarise(Count = n()) %&gt;%
    summarise(AvgSecurity = mean(sec_engagement)) %&gt;%
    summarise(AvgPartviolence = mean(part_violence))
  </code></pre>
  <p>I have no issue when I run <code>summarise(Count = n())</code>, but I can't get running <code>summarise(AvgSecurity = mean(sec_engagement))</code> and <code> summarise(AvgPartviolence = mean(part_violence))</code> to work. Any advice would be appreciated. Below are some data for your convenience.</p>
  <pre><code>structure(list(cowcode = c(40L, 40L, 40L, 40L, 40L, 40L), event_date = structure(c(12183, 
  15302, 12173, 12173, 12393, 12583), class = &quot;Date&quot;), side = c(0L, 
  1L, 0L, 0L, 0L, 0L), scope = c(0L, 0L, 0L, 0L, 0L, 0L), part_violence = c(NA_integer_, 
  NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
  ), sec_engagement = c(NA_integer_, NA_integer_, NA_integer_, 
  NA_integer_, NA_integer_, NA_integer_), numparticipants = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;2000&quot;, &quot;&quot;), year = c(2003, 2011, 2003, 2003, 2003, 
  2004)), row.names = c(NA, 6L), class = &quot;data.frame&quot;)
  </code></pre>