为了获得两组分数的平均值的绝对偏差,我通常需要在R中编写长代码,如下所示。
我想知道在BASE R中是否有可能Vectorize
mad()
Vectorized
函数,以便与示例I中的每组分数的平均分数的绝对偏差可以使用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
获得以下显示?其他任何可行的想法都受到高度赞赏吗?
section .data
qVar1: dq 1
section .bss
var28: resb 28
section .text
_main:
; Use an MMX instruction
movq mm0, [qVar1] ; Move quadword from r/m64 to mm.
; Read Tag Word
fstenv [var28]
mov ax, [var28 + 8] ; move the Tag Word to ax
答案 0 :(得分:2)
怎么样
score <- lapply(split(y, groups), FUN = function (u) abs(u - mean(u)))
或
score <- ave(y, groups, FUN = function (u) abs(u - mean(u)))
结果以不同的方式组织。选择最适合你的那个。
你的措辞有问题。 mad
为数据返回单个统计信息/值。例如,
sapply(split(y, groups), mad)
您没有对mad
进行矢量化,而只是计算每个数据的偏差,如示例代码所示。
答案 1 :(得分:1)
如果你把所有东西都放在data.frame中,那就更清洁了。在基地R,
set.seed(0)
df <- data.frame(y = rnorm(20),
group = rep(1:2, each = 10))
df$abs_dev <- with(df, ave(y, group, FUN = function(x){abs(mean(x) - x)}))
df
#> y group abs_dev
#> 1 1.262954285 1 0.90403032
#> 2 -0.326233361 1 0.68515732
#> 3 1.329799263 1 0.97087530
#> 4 1.272429321 1 0.91350536
#> 5 0.414641434 1 0.05571747
#> 6 -1.539950042 1 1.89887401
#> 7 -0.928567035 1 1.28749100
#> 8 -0.294720447 1 0.65364441
#> 9 -0.005767173 1 0.36469114
#> 10 2.404653389 1 2.04572943
#> 11 0.763593461 2 1.12607477
#> 12 -0.799009249 2 0.43652794
#> 13 -1.147657009 2 0.78517570
#> 14 -0.289461574 2 0.07301974
#> 15 -0.299215118 2 0.06326619
#> 16 -0.411510833 2 0.04902952
#> 17 0.252223448 2 0.61470476
#> 18 -0.891921127 2 0.52943981
#> 19 0.435683299 2 0.79816461
#> 20 -1.237538422 2 0.87505711
或dplyr,
library(dplyr)
set.seed(0)
df <- data_frame(y = rnorm(20),
group = rep(1:2, each = 10))
df <- df %>% group_by(group) %>% mutate(abs_dev = abs(mean(y) - y))
df
#> # A tibble: 20 x 3
#> # Groups: group [2]
#> y group abs_dev
#> <dbl> <int> <dbl>
#> 1 1.262954285 1 0.90403032
#> 2 -0.326233361 1 0.68515732
#> 3 1.329799263 1 0.97087530
#> 4 1.272429321 1 0.91350536
#> 5 0.414641434 1 0.05571747
#> 6 -1.539950042 1 1.89887401
#> 7 -0.928567035 1 1.28749100
#> 8 -0.294720447 1 0.65364441
#> 9 -0.005767173 1 0.36469114
#> 10 2.404653389 1 2.04572943
#> 11 0.763593461 2 1.12607477
#> 12 -0.799009249 2 0.43652794
#> 13 -1.147657009 2 0.78517570
#> 14 -0.289461574 2 0.07301974
#> 15 -0.299215118 2 0.06326619
#> 16 -0.411510833 2 0.04902952
#> 17 0.252223448 2 0.61470476
#> 18 -0.891921127 2 0.52943981
#> 19 0.435683299 2 0.79816461
#> 20 -1.237538422 2 0.87505711
或data.table:
library(data.table)
set.seed(0)
dt <- data.table(y = rnorm(20),
group = rep(1:2, each = 10))
dt[, abs_dev := abs(mean(y) - y), by = group][]
#> y group abs_dev
#> 1: 1.262954285 1 0.90403032
#> 2: -0.326233361 1 0.68515732
#> 3: 1.329799263 1 0.97087530
#> 4: 1.272429321 1 0.91350536
#> 5: 0.414641434 1 0.05571747
#> 6: -1.539950042 1 1.89887401
#> 7: -0.928567035 1 1.28749100
#> 8: -0.294720447 1 0.65364441
#> 9: -0.005767173 1 0.36469114
#> 10: 2.404653389 1 2.04572943
#> 11: 0.763593461 2 1.12607477
#> 12: -0.799009249 2 0.43652794
#> 13: -1.147657009 2 0.78517570
#> 14: -0.289461574 2 0.07301974
#> 15: -0.299215118 2 0.06326619
#> 16: -0.411510833 2 0.04902952
#> 17: 0.252223448 2 0.61470476
#> 18: -0.891921127 2 0.52943981
#> 19: 0.435683299 2 0.79816461
#> 20: -1.237538422 2 0.87505711