Adrian
Adrian

Reputation: 9793

R: calculate the row mean in a matrix

I have a matrix that looks something like this:

> 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

I want to calculate the average of misclassification rate, type1 and type2 errors. I tried apply(matrix, 1, mean) but that gave me the following error:

> 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
> 

Upvotes: 3

Views: 6402

Answers (2)

INDRAJITH EKANAYAKE
INDRAJITH EKANAYAKE

Reputation: 4274

Your matrix is more like data frame for me but the question is about to calculate the row mean in a matrix. So let me take an example matrix named A and calculate the average of the second row. Hope this will helpful for you.

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])

Upvotes: 0

Rich Scriven
Rich Scriven

Reputation: 99321

You've got list items as matrix elements, which is/will be troublesome. If mat is your matrix, we can see that the first column is a list.

str(mat[,1])
# List of 3
#  $ misclassification.rate: num 0.227
#  $ type1.error           : num 0.104
#  $ type2.error           : num 0.447

This can occur as a result of calling *bind() after as.list(). For example,

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   

which is a matrix, but has list elements as rows and columns.

It would be best to try and clear that up before you get to this point, if you can. If you can't go back and fix it in the code, you can adjust mat into a proper matrix, then do the calculation.

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

Now m is a 3x10 matrix with numeric elements. Alternatively, you could turn it into a 10x3 matrix with

apply(mat, 1, unlist)

But it's best to find out what caused it and sort that out.

Upvotes: 5

Related Questions