R:计算矩阵中的行平均值

R:计算矩阵中的行平均值,r,R,我有一个矩阵,看起来像这样: > dput(matrix) structure(list(0.226984126984127, 0.104133986928105, 0.446807359307359, 0.231216931216931, 0.103735527010194, 0.464679487179487, 0.223544973544974, 0.108543233082707, 0.430808080808081, 0.23809523809523

我有一个矩阵,看起来像这样:

> dput(matrix)
structure(list(0.226984126984127, 0.104133986928105, 0.446807359307359, 
    0.231216931216931, 0.103735527010194, 0.464679487179487, 
    0.223544973544974, 0.108543233082707, 0.430808080808081, 
    0.238095238095238, 0.120502226531638, 0.436919746919747, 
    0.242328042328042, 0.117595073914733, 0.467496392496393, 
    0.23452380952381, 0.115559100902687, 0.426222943722944, 0.231216931216931, 
    0.112887365472505, 0.441438006438006, 0.231878306878307, 
    0.0990079365079365, 0.471089743589744, 0.230952380952381, 
    0.123904761370605, 0.414044844044844, 0.226984126984127, 
    0.111960047176765, 0.435427627927628), .Dim = c(3L, 10L), .Dimnames = list(
    c("misclassification.rate", "type1.error", "type2.error"), 
    NULL))

> matrix
                       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10]
misclassification.rate 0.227 0.231 0.224 0.238 0.242 0.235 0.231 0.232 0.231 0.227
type1.error            0.104 0.104 0.109 0.121 0.118 0.116 0.113 0.099 0.124 0.112
type2.error            0.447 0.465 0.431 0.437 0.467 0.426 0.441 0.471 0.414 0.435
我想计算错误分类率、类型1和类型2错误的平均值。我尝试了
应用(矩阵,1,平均值)
,但这给了我以下错误:

> apply(matrix, 1, mean)
misclassification.rate            type1.error            type2.error 
                    NA                     NA                     NA 
Warning messages:
1: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
2: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
3: In mean.default(newX[, i], ...) :
  argument is not numeric or logical: returning NA
> 

您已经将列表项作为矩阵元素,这是/将是麻烦的。如果
mat
是您的矩阵,我们可以看到第一列是一个列表

str(mat[,1])
# List of 3
#  $ misclassification.rate: num 0.227
#  $ type1.error           : num 0.104
#  $ type2.error           : num 0.447
这可能是在
as.list()
之后调用
*bind()
的结果。比如说,

rbind(as.list(1:5), as.list(20:24), as.list(2:6))
#      [,1] [,2] [,3] [,4] [,5]
# [1,] 1    2    3    4    5   
# [2,] 20   21   22   23   24  
# [3,] 2    3    4    5    6   
它是一个矩阵,但具有作为行和列的列表元素

如果可以的话,最好在你达到这一点之前把它弄清楚。如果无法返回并在代码中修复它,可以将
mat
调整为适当的矩阵,然后进行计算

m <- matrix(unlist(mat), nrow(mat), dimnames = dimnames(mat))
rowMeans(m)
# misclassification.rate            type1.error            type2.error 
#              0.2317725              0.1117829              0.4434934 

但最好找出原因并进行分类。

对我来说,你的矩阵更像是数据框,但问题是要计算矩阵中的行平均值。让我以一个名为
A
的矩阵为例,计算第二行的平均值。希望这对你有帮助

A=matrix(c(90,67,51,95,64,59,92,61,67,93,83,43),4,3,byrow = TRUE)
A
#avg of the second row
mean(A[,2])

对我不起作用
>rowMeans(矩阵)base::rowMeans(x,na.rm=na.rm,dims=dims,…):“x”必须是数字的
你最初是怎么想到这个结构的?这是一件非常不寻常的东西。你是从
aggregate
中得到的吗?很好的解决方案,不过我想知道最初是如何得到这个奇怪的矩阵的
A=matrix(c(90,67,51,95,64,59,92,61,67,93,83,43),4,3,byrow = TRUE)
A
#avg of the second row
mean(A[,2])