简体   繁体   English

从 R 列表中的多个矩阵访问相同的矩阵 position?

[英]Accessing same matrix position from multiple matrices in a list in R?

I have a List of arbitrary length, all entries being matrices of arbitrary length and width but identically sized to each other.我有一个任意长度的列表,所有条目都是任意长度和宽度的矩阵,但彼此大小相同。 I would like to compute the mean, median, and mode of the values at the same position in every matrix in the list.我想计算列表中每个矩阵中相同 position 值的平均值、中位数和众数。 I could do this with a for loop, but is there a simpler/vectorized way?我可以用 for 循环来做到这一点,但有没有更简单/矢量化的方式?

I tried:我试过了:

for (x in 1:X){
   for (y in 1:Y){
      My_List[[B+1]][y,x] <- mean(My_List[[1:B]][y,x])
      My_List[[B+2]][y,x] <- median(My_List[[1:B]][y,x])
      My_List[[B+3]][y,x] <- mode(My_List[[1:B]][y,x])
   }
}

Where the list is of length B+3, and the matrices are all of width X and length Y, but I got errors relating to trying to access across list entries.列表的长度为 B+3,矩阵的宽度均为 X,长度为 Y,但我遇到了与尝试跨列表条目访问有关的错误。 I considered lapply(), but I don't know how to turn a sunsetting operation into a FUN for the apply() family.我考虑过 lapply(),但我不知道如何将日落操作变成 apply() 系列的乐趣。 I could do this with an obscene nest of for loops, but there must be a better way, especially in R.我可以用一个淫秽的 for 循环嵌套来做到这一点,但必须有更好的方法,尤其是在 R 中。

EDIT: Reproducible example and expected output:编辑:可重现的示例和预期的 output:

Step 1: Generation of My_List:第 1 步:生成 My_List:

Matrix_A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
Matrix_B <- matrix(c(2, 1, 3, 3, 6, 5), nrow = 2)
Matrix_C <- matrix(c(1, 1, 3, 3, 5, 5), nrow = 2)

# X = 3, Y = 2

B = 3

My_List <- vector(mode = "list", length = B + 3)

My_List[[1]] <- Matrix_A
My_List[[2]] <- Matrix_B
My_List[[3]] <- Matrix_C

My_List

Step 1 Output, note the NULL placeholder spaces:步骤 1 Output,注意 NULL 占位符空格:

[[1]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[2]]
     [,1] [,2] [,3]
[1,]    2    3    6
[2,]    1    3    5

[[3]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    1    3    5

[[4]]
NULL

[[5]]
NULL

[[6]]
NULL

Using the same code block as above, I expect to see My_List[[4]][1,1] be the mean of My_List[[1]][1,1], My_List[[2]][1,1], and My_List[[3]][1,1], so 1 + 2 + 1 / 3 = 1.333, and so on for every combination of y and x.使用与上面相同的代码块,我希望看到 My_List[[4]][1,1] 是 My_List[[1]][1,1], My_List[[2]][1,1] 的平均值, 和 My_List[[3]][1,1],所以 1 + 2 + 1 / 3 = 1.333,对于 y 和 x 的每个组合,依此类推。 Index 4 of the list will be for mean, 5 for median, and 6 for mode.列表的索引 4 表示平均值,5 表示中位数,6 表示众数。

I expect the final output to be:我希望最终的 output 是:

[[1]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[2]]
     [,1] [,2] [,3]
[1,]    2    3    6
[2,]    1    3    5

[[3]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    1    3    5

[[4]] # Mean
     [,1]     [,2]     [,3]
[1,]    1.333    3.000    5.333
[2,]    1.333    3.333    5.333

[[5]] # Median
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    1    3    5

[[6]] # Mode
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    1    3    5

You could transform the list of matrices to a 3 dimensional array:您可以将矩阵列表转换为 3 维数组:

i <- 2
j <- 3

n <- 4

# create list of n i*j matrices
l <- replicate(n, list(matrix(sample(10,i*j),i,j)))

# list to 3D array
arr.3d <- array(unlist(l), c(i, j, n))

apply(arr.3d, 1:2, mean)
     [,1] [,2] [,3]
[1,] 6.50 5.00 3.50
[2,] 5.25 4.25 6.75

apply(arr.3d, 1:2,median)
     [,1] [,2] [,3]
[1,]    6    5  1.5
[2,]    5    4  6.0

For mode :对于模式

Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

apply(arr.3d,1:2,Mode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM