-1

I have a data frame that contains two types cols - numeric and factor. How can I difference each element with group mean in a new column. I can find group mean with 'aggregate', but I can't extract diff between each element and it's group mean (different lenght).

Bax Baxov
  • 29
  • 5
  • When asking for programming help, it's expected that you include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and desired output. – MrFlick Oct 14 '16 at 15:56

1 Answers1

2

You can easily do it with a basic linear model (I removed the intercept so that the value of each effect is the group mean in the model summary). Let's take the example of data(mtcars), the variable am being your grouping factor and mpg your numeric variable:

mtcars$am <- factor(mtcars$am)
lm(mpg~am-1, mtcars)$resid

The residuals of this model are by definition the difference between the element and its group mean.

agenis
  • 8,069
  • 5
  • 53
  • 102