On Mar 27, 2011, at 3:22 AM, peter dalgaard wrote:
On Mar 27, 2011, at 08:25 , David Winsemius wrote:
On Mar 26, 2011, at 10:26 PM, fisken wrote:
I was wondering if it is possible to do the following in a smarter
way.
I want get the mean value across the columns of a matrix, but I want
_along_ the columns, I assume.
to do this on subrows of the matrix, given by some vector(same
length
as the the number of rows). Something like
nObs<- 6
nDim <- 4
m <- matrix(rnorm(nObs*nDim),ncol=nDim)
fac<-sample(1:(nObs/2),nObs,rep=T)
##loop trough different 'factor' levels
for (i in unique(fac))
print(apply(m[fac==i,],2,mean))
This would be a lot simpler and faster:
colMeans(m[unique(fac),])
#[1] 1.3595197 -0.1374411 0.1062527 -0.3897732
Say what??? (I suspect David needs to get his sleep - or coffee, if
he is in Europe.)
At that point it was sleep that I needed. Now .... trying to decide
if I should just go back to bed or make coffee.
How about:
aggregate(m,list(fac),mean)
Group.1 V1 V2 V3 V4
1 1 -0.03785420 -0.2573805 -0.3025759 0.006999996
2 2 -1.39961300 0.2296900 -0.1122359 -0.302734531
3 3 0.50886649 0.6546153 -0.4270368 -0.411807709
by(m,list(fac),colMeans)
: 1
V1 V2 V3 V4
-0.037854195 -0.257380542 -0.302575901 0.006999996
-------------------------------------------------------------
: 2
V1 V2 V3 V4
-1.3996130 0.2296900 -0.1122359 -0.3027345
-------------------------------------------------------------
: 3
V1 V2 V3 V4
0.5088665 0.6546153 -0.4270368 -0.4118077
--
Peter Dalgaard
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.