title: Sum of rows under multiple conditions in R
body: <p>I have a data frame with many variables, two of them are "year" and "country". I would like to sum rows of some columns under a condition that rows are for a country in a particular year and NA's for those that are not specified. For example:</p>
  
  <pre><code>A B C  year  country total
  1 1 1  2000   IT      3
  2 2 2  2001   IT      6
  3 3 3  2001   DE      9
  4 4 4  2002   DK      NA
  5 5 5  2000   FR      NA
  6 6 6  2001   DE      18
  </code></pre>
  
  <p>In Stata this would look like:</p>
  
  <pre><code>egen variable = rowtotal (A B C) if ///
  country_year=="36_04" | country_year=="37_04" | country_year=="96_04" | ///
  country_year=="97_04" | country_year=="83_04" | country_year=="83_09" | ///
  country_year=="87_09" | country_year=="87_04"
  </code></pre>