gpt4 book ai didi

r - data.table:从值中减去水平意味着

转载 作者:行者123 更新时间:2023-12-04 11:22:59 26 4
gpt4 key购买 nike

我想知道如何从 data.table 中的值中减去水平平均值。下面给出了我的 MWE。

set.seed(12345)
A <- rep(x=paste0("A", 1:2), each=6)
B <- rep(x=paste0("B", 1:3), each=2, times=2)
Rep <- rep(x=1:2, times=3)
Y <- rnorm(n=12, mean = 50, sd = 5)

library(data.table)
dt <- data.table(A, B, Rep, Y)

dt[, j=mean(Y), by=.(A, B)]
dt[, j=mean(Y), by=.(A)]

dt[, j=mean(Y), by=.(A, B)] - dt[, j=mean(Y), by=.(A)]

Error in Ops.data.frame(dt[, j = mean(Y), by = .(A, B)], dt[, j = mean(Y), :
‘-’ only defined for equally-sized data frames

已编辑

我想要这个:

dt[, j=mean(Y), by=.(A, B)] - dt[, j=mean(Y), by=.(A)] - dt[, j=mean(Y), by=.(B)] + dt[, j=mean(Y)]

最佳答案

你得到错误的原因是你减去的两个 data.table 的维度不同。另一方面,您可以使用 data.table 进行链式转换:

dt[, j := mean(Y), .(A, B)][, j := j - mean(Y), .(A)]
dt
A B Rep Y j
1: A1 B1 1 52.92764 3.6373822
2: A1 B1 2 53.54733 3.6373822
3: A1 B2 1 49.45348 -1.0071061
4: A1 B2 2 47.73251 -1.0071061
5: A1 B3 1 53.02944 -2.6302761
6: A1 B3 2 40.91022 -2.6302761
7: A2 B1 1 53.15049 0.1752053
8: A2 B1 2 48.61908 0.1752053
9: A2 B2 1 48.57920 -3.7182851
10: A2 B2 2 45.40339 -3.7182851
11: A2 B3 1 49.41876 3.5430798
12: A2 B3 2 59.08656 3.5430798

对于更新,你可以这样做:

dt[, j := mean(Y), .(A, B)][, j := j - mean(Y), .(A)][, j := j - mean(Y), .(B)][, j := j + mean(Y)]
dt
A B Rep Y j
1: A1 B1 1 52.92764 1.731088
2: A1 B1 2 53.54733 1.731088
3: A1 B2 1 49.45348 1.355590
4: A1 B2 2 47.73251 1.355590
5: A1 B3 1 53.02944 -3.086678
6: A1 B3 2 40.91022 -3.086678
7: A2 B1 1 53.15049 -1.731088
8: A2 B1 2 48.61908 -1.731088
9: A2 B2 1 48.57920 -1.355590
10: A2 B2 2 45.40339 -1.355590
11: A2 B3 1 49.41876 3.086678
12: A2 B3 2 59.08656 3.086678

关于r - data.table:从值中减去水平意味着,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883056/

26 4 0