简体   繁体   English

计算R中矩阵中设置的同名和行名的平均值

[英]Calculate mean of colnames and rownames set in matrix in R

Apologies if this has been asked elsewhere but I couldn't find an answer. 抱歉,是否有人在其他地方提出过此要求,但我找不到答案。

I'm attempting to calculate the mean of a matrix I've created, using colMeans and rowMeans. 我正在尝试使用colMeans和rowMeans计算我创建的矩阵的平均值。 The mean values are what I want to populate the matrix with, and have set the rows and columns using vectors 平均值是我要用来填充矩阵的值,并且已经使用矢量设置了行和列

Currently my code looks like this: 目前,我的代码如下所示:

A = matrix(ncol = (14), # number of columns in matrix
       nrow = (10), # number of rows in matrix
       colMeans(A, na.rm = FALSE, dims = colnames(A)),
       rowMeans(A, na.rm = FALSE, dims = rownames(A)),
       byrow = TRUE)
colnames(A) c("2","4","6","8","10","12","14","16","18","20","22","24","26","28")
rownames(A) <- c("10","20","30","40","50","60","70","80","90","100")

A

Running this returns a matrix with NA values. 运行此命令将返回具有NA值的矩阵。

Expected output would be a matrix like below, where NA values are replaced by the mean of the column and row numbers. 预期的输出将是一个如下所示的矩阵,其中NA值由列数和行数的平均值代替。

For example the first intersection would equal 6, as 2+10/2 例如,第一个交集等于6,即2 + 10/2

    2  4  6  8 10 12 14 16 18 20 22 24 26 28
10  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
20  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
30  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
40  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
50  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
60  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
70  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
80  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
90  NA NA NA NA NA NA NA NA NA NA NA NA NA NA
100 NA NA NA NA NA NA NA NA NA NA NA NA NA NA

This is essentially what you want using the outer function. 本质上,这就是您要使用outer函数所需要的。 Note that t transposes the matrix so it's the right way round that you specify above.It could also just be outer(B,A, fn) 请注意, t转置矩阵,因此这是您在上面指定的正确方法,它也可以是external outer(B,A, fn)

A<-seq(2,28,2)
B<-seq(10,100,10)
fn<-function(A,B)(A+B)/2
t(outer(A,B, fn))


      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
 [1,]    6    7    8    9   10   11   12   13   14    15    16    17    18    19
 [2,]   11   12   13   14   15   16   17   18   19    20    21    22    23    24
 [3,]   16   17   18   19   20   21   22   23   24    25    26    27    28    29
 [4,]   21   22   23   24   25   26   27   28   29    30    31    32    33    34
 [5,]   26   27   28   29   30   31   32   33   34    35    36    37    38    39
 [6,]   31   32   33   34   35   36   37   38   39    40    41    42    43    44
 [7,]   36   37   38   39   40   41   42   43   44    45    46    47    48    49
 [8,]   41   42   43   44   45   46   47   48   49    50    51    52    53    54
 [9,]   46   47   48   49   50   51   52   53   54    55    56    57    58    59
[10,]   51   52   53   54   55   56   57   58   59    60    61    62    63    64

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

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