title: How to calculate the mean in a data frame using aggregate function in R?
body: <p>I have a data frame df1:</p>
  
  <pre><code>number=c(4,3,2,3,4,1)
  year=c("2000","2000","2000", "2015", "2015", "2015")
  items=c(12, 10, 15, 5, 10, 7)
  df1=data.frame(number, year, items)
  setDT(df1)[, Prop := number/sum(number), by = year]
  </code></pre>
  
  <p>such that it looks like this:</p>
  
  <pre><code>  number year items      Prop
  1:      4 2000    12 0.4444444
  2:      3 2000    10 0.3333333
  3:      2 2000    15 0.2222222
  4:      3 2015     5 0.3750000
  5:      4 2015    10 0.5000000
  6:      1 2015     7 0.1250000
  </code></pre>
  
  <p>I want to get the mean of the number of items per year, so I tried using this fuction:</p>
  
  <pre><code>mean.df1=aggregate((df1$number*df1$Prop),list(df1$year), mean)
  </code></pre>
  
  <p>but it returns the wrong values for the mean. I want it to return:</p>
  
  <pre><code>  Group.1        x
  1    2000 2.918918
  2    2015 2.296296
  </code></pre>
  
  <p>where Group.1 is the year and x is the correct mean.</p>
  
  <p>Thanks!</p>