Reputation: 65
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
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
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
Reputation: 4614
Function colMeans
is right for that:
> colMeans(dta)
[1] 11.5 9.5 11.5
Upvotes: 4