title: R: Replacing NA values by mean of hour with dplyr
body: <p>I'm learning the dplyr package in R and I really like it. But now I'm dealing with NA values in my data. </p>
  
  <p>I would like to replace any NA by the average of the corresponding hour, for example with this very easy example:</p>
  
  <pre><code>#create an example
  day = c(1, 1, 2, 2, 3, 3)
  hour = c(8, 16, 8, 16, 8, 16)
  profit = c(100, 200, 50, 60, NA, NA)
  shop.data = data.frame(day, hour, profit)
  
  #calculate the average for each hour
  library(dplyr)
  mean.profit &lt;- shop.data %&gt;%
    group_by(hour) %&gt;%
    summarize(mean=mean(profit, na.rm=TRUE))
  
  &gt; mean.profit
  Source: local data frame [2 x 2]
  
    hour mean
  1    8   75
  2   16  130
  </code></pre>
  
  <p>Can I use the dplyr transform command to replace the NA's of day 3 in the profit with 75 (for 8:00) and 130 (for 16:00)?</p>