title: aggregate function in R, multiple conditions
body: <p>I don't get the right way to compute a new column which contains a sum up of a column in a data frame by aggregating anothor column by a grep function. The data frame looks like follows:</p>
  
  <pre><code>   ID_Oeffnungszeit ID_Einrichtung Anzahl_Std Bez_Oeffnungszeit
                   1        3000001       0.50  Montag Vormittag
                   1        3000003       3.00  Montag Vormittag
                   1        3000008       2.50  Montag Vormittag
                   2        3000001       1.00  Montag Nachmittag
                   4        3000003       1.50  Dienstag Vormittag
                   1        3000023       1.00  Montag Vormittag
                   1        3000025       1.00  Montag Vormittag
                   1        3000026       1.00  Montag Vormittag
                   3        3000001       2.00  Montag Abend
                              ...
                   1        3000038       3.50  Montag Vormittag
                            and so on...
  </code></pre>
  
  <p>There are 21 characteristics for the variable <code>Bez_Oeffnungszeit</code>, but they can be categorized due to the fact if they contain the words <code>Vormittag</code>, <code>Nachmittag</code> or <code>Abend</code>.
  So I would like to generate a new column which contains the sums of the column <code>Anzahl_Std</code> for each <code>ID_Einrichtung</code> depending on which word the <code>Bez_Oeffnungszeit</code> contains.</p>
  
  <p>I'm quite sure that I should use the <code>grep</code> and the <code>by</code> functions, but I couldn't manage to make the it work in my sense...</p>
  
  <pre><code>df &lt;- structure(list(ID_Oeffnungszeit = c("1", "1", "1", "2", "4", 
  "1", "1", "1", "3"), ID_Einrichtung = c(3000001L, 3000003L, 3000008L, 
  3000001L, 3000003L, 3000023L, 3000025L, 3000026L, 3000001L), 
      Anzahl_Std = c(0.5, 3, 2.5, 1, 1.5, 1, 1, 1, 2), Bez_Oeffnungszeit = c("Montag Vormittag", 
      "Montag Vormittag", "Montag Vormittag", "Montag Nachmittag", 
      "Dienstag Vormittag", "Montag Vormittag", "Montag Vormittag", 
      "Montag Vormittag", "Montag Abend")), .Names = c("ID_Oeffnungszeit", 
  "ID_Einrichtung", "Anzahl_Std", "Bez_Oeffnungszeit"), class = "data.frame", row.names = c(NA, 
  -9L))
  </code></pre>