2
这里是我的小数据集表工作 - 功能与保证金资金和个体值
set.seed(123)
X1 <- rep(1:3, each = 4)
X2 <- c(1:4, 1:4, 1:4)
Y <- rnorm (12, 4, 20)
myd <- data.frame(X1, X2, Y)
# "unmelten" form of the dataframe
aggregate(Y ~ X1, myd, 'c')
X1 Y.1 Y.2 Y.3 Y.4
1 1 -7.2095129 -0.6035498 35.1741663 5.4101678
2 2 6.5857547 38.3012997 13.2183241 -21.3012247
3 3 -9.7370570 -4.9132394 28.4816359 11.1962765
的实际数据矩阵在尺寸上更大的工作。我想编写使用melten数据形成到这样一个数学函数:
Example for X1, Y1.1 cell
(X1,Y.1) - sum(X1 row) - sum(Y.1 column) + grand total (= sum(Y))
-7.2095129 - (-7.2095129 - 0.6035498 + 35.1741663 + 5.4101678) - (-7.2095129 + 6.5857547 -9.7370570) + sum (all values in the able)
另外两个例子,我的观点明确。
For X2, Y1.1 cell
(X2,Y.1) - sum(X2 row) - sum(Y.1 column) + grand total (= sum(Y))
For X3, Y.3 cell
(X3,Y.3) - sum(X3 row) - sum(Y.3 column) + grand total (= sum(Y))
同样这将输出3×4大小的对矩阵中的每个单元的矩阵。
我想写一个函数来做到这一点,但可以得到任何想法。请帮助