【发布时间】:2017-06-24 16:51:03
【问题描述】:
要获得两组分数的绝对偏差,我通常需要用 R 编写长代码,如下所示。
问题
我想知道是否有可能在 BASE R 中以某种方式 Vectorize mad() 函数,以便我在下面显示的示例中的每组分数的平均分数的绝对偏差可以使用获得Vectorized 版本的mad()?任何其他可行的想法都受到高度赞赏?
set.seed(0)
y = as.vector(unlist(mapply(FUN = rnorm, n = c(10, 10)))) # Produces two sets of scores
groups = factor( rep(1:2, times = c(10, 10) ) ) # Grouping ID variable
G1 = y[groups == 1] # subset y scores for group 1
G2 = y[groups == 2] # subset y scores for group 2
G1.abs.dev = abs(G1 - mean(G1)) # absolute deviation from mean scores for group 1
G2.abs.dev = abs(G2 - mean(G2)) # absolute deviation from mean scores for group 2
【问题讨论】:
标签: r function statistics