title: Average deviation from several columns based on a single column in a dataframe in R
body: <p>This is my first post on here and I am pretty new to R. 
  I have a huge datafile that looks like the example below.</p>
  
  <pre><code>&gt; name = factor(c("A","B","C","D","E","F","G","H","H"))
  &gt;  school = c(1,1,1,2,2,2,3,3,3)
  &gt;  age = c(10,20,0,30,40,50,60,NA,70)
  &gt;  mark = c(100,70,100,50,90,100,NA,50,50)
  &gt;  data = data.frame(name=name,school=school,age=age)
  
  name   school   age mark   (many other trait columns)
  A      1        10   100
  B      1        20    70
  C      1        NA   100
  D      2        30    50
  E      2        40    90
  F      2        50   100
  G      3        60    NA
  H      3        NA    50
  H      3        70    50
  </code></pre>
  
  <p>What I need to do is calculate the average of many traits per school and for each trait I want to create to other columns, one with the mean per school for the trait and another one with the average deviation. I also have trait values of "zero" and "NA", which I dont want to include in the mean calculation. The file I need would look like this:</p>
  
  <pre><code>name school   age   agemean  agedev  mark   markmean  markdev  (continue for other traits)
  A      1      10        15     -5     100     90       10
  B      1      20        15      5      70     90      -20
  C      1       0        15      0     100     90       10
  D      2      30        40    -10      50     80      -30
  E      2      40        40      0      90     80       10
  F      2      50        40     10     100     80       20
  G      3      60        65     -5      NA     50        0
  H      3      NA        65      0      50     50        0
  H      3      70        65      5      50     50        0
  </code></pre>
  
  <p>I did a search on here and found some similar questions, but I didnt get how to apply to my case. I tried to use the agreggate function, but it is not working. Any help would be very much appreciated. 
  Cheers.</p>