user3407190
user3407190

Reputation: 65

How to find the means of all columns of a matrix simultaneously

I have a matrix and I want to find the means of all the columns at the same time. Can anyone help do this? Below is my data.

dta=matrix(c(11,12,10,9,15,8),nrow=2,ncol=3)

How do I find the means of all columns simultaneously.

Upvotes: 0

Views: 72

Answers (3)

Fernando
Fernando

Reputation: 7895

Just to remember that you can also use apply over the columns:

dta = matrix(c(11, 12, 10, 9, 15, 8), nrow = 2, ncol = 3)
cmeans = apply(dta, 2, mean)
print(cmeans)

[1] 11.5  9.5 11.5

Upvotes: 0

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Just use colMeans:

> dta=matrix(c(11,12,10,9,15,8),nrow=2,ncol=3)
> dta
     [,1] [,2] [,3]
[1,]   11   10   15
[2,]   12    9    8
> colMeans(dta)
[1] 11.5  9.5 11.5

Upvotes: 2

Andrey Shabalin
Andrey Shabalin

Reputation: 4614

Function colMeans is right for that:

> colMeans(dta)
[1] 11.5  9.5 11.5

Upvotes: 4

Related Questions