使用 R 中的 apply() 函数查找每行 2 个矩阵的加权特征
Find weighted characteristics for each row of 2 matrices using apply() function in R
我有 2 个矩阵,我想使用 apply()
为每个矩阵应用加权中位数、加权 iqr 和加权分位数 row.I 想要这样的东西:
apply(z, 1, function(x) weighted.mean(x[1:3], x[4:6]))
w<- matrix(c(3, 0, 0, 0,
0, 1, 2, 0,
1, 0, 1, 1,
0, 3, 0, 0,
0, 0, 2, 1,
1, 0, 1, 1), nrow = 6, ncol = 6)
x<- matrix(c(-1.569192, 0.0000000, 0, 0.0000000,
0.000000, -0.6744898, 0, 0.0000000,
0.000000, 0.0000000, 0, 0.0000000,
0.000000, 0.0000000, 0, 0.6744898,0,0,0,0,0,0,0,0), nrow = 6, ncol = 6)
要使用具有相同索引作为权重的 w 行对 x 的每一行取加权平均值,您可以执行 apply(cbind(x, w), 1, function(x) {weighted.mean(x[1:6], x[7:12])})
。
我有 2 个矩阵,我想使用 apply()
为每个矩阵应用加权中位数、加权 iqr 和加权分位数 row.I 想要这样的东西:
apply(z, 1, function(x) weighted.mean(x[1:3], x[4:6]))
w<- matrix(c(3, 0, 0, 0,
0, 1, 2, 0,
1, 0, 1, 1,
0, 3, 0, 0,
0, 0, 2, 1,
1, 0, 1, 1), nrow = 6, ncol = 6)
x<- matrix(c(-1.569192, 0.0000000, 0, 0.0000000,
0.000000, -0.6744898, 0, 0.0000000,
0.000000, 0.0000000, 0, 0.0000000,
0.000000, 0.0000000, 0, 0.6744898,0,0,0,0,0,0,0,0), nrow = 6, ncol = 6)
要使用具有相同索引作为权重的 w 行对 x 的每一行取加权平均值,您可以执行 apply(cbind(x, w), 1, function(x) {weighted.mean(x[1:6], x[7:12])})
。