I have a matrix like this:
mt1=matrix(1:144,ncol=12,nrow=12)
And I would like only to calcutate the mean only for 3 values in a row. These are the three first values:
[,1] [,2] [,3]
[1,] 1 13 25
the mean should be 13
.
And the same for the next three values and the same for the next three values.. And so on and on until the end of rows.
I know I have to use the apply
function, but I cannot figure out to apply the mean only to specific values that is only to a sequence of rows, and then on the next sequence of rows, and get then as a result a vector with the means of only 3 values per row.
Thanks a lot!
Try:
> apply(mm, 1, function(x) mean(x[1:3]))
[1] 13 14 15 16 17 18 19 20 21 22 23 24
Mean of 1,13,25 is not 29 but 13
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments