Bax Baxov - 7 months ago 33

R Question

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).

Answer

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.