Obtaining absolute deviation from mean for two sets of scores

后端 未结 2 1166
旧巷少年郎
旧巷少年郎 2021-01-16 10:28

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.

Question

I was won

2条回答
  •  孤城傲影
    2021-01-16 10:34

    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.

提交回复
热议问题