To obtain absolute deviation from the mean for two groups of scores, I usually need to write long codes in R such as the ones shown below.
I was won
How about
score <- lapply(split(y, groups), FUN = function (u) abs(u - mean(u)))
or
score <- ave(y, groups, FUN = function (u) abs(u - mean(u)))
The results are organized in a different way. Choose the one that is most comfortable to you.
There is something wrong with your wording. mad
returns a single statistic / value for data. For example,
sapply(split(y, groups), mad)
You are not vectorizing mad
, but simply computing the deviation for each datum as your example code shows.