Message-ID: <1345283747.15753.YahooMailNeo@web142602.mail.bf1.yahoo.com>
Date: 2012-08-18T09:55:47Z
From: arun
Subject: Row means of matrix.
In-Reply-To: <CAAuJZX+y_XFdmRQ4V7ZVigw8NP=K8vNQN6zMxTf8eMLpispmpw@mail.gmail.com>
HI,
Try this:
?m[m==0]<-NA
apply(m,1,mean,na.rm=T)
#[1] 2.5 NaN 1.0
A.K.
----- Original Message -----
From: Yingwei Lee <dingayinga at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, August 17, 2012 11:28 PM
Subject: [R] Row means of matrix.
Hi all,
? ? ? ? ? I'm just having trouble getting row means of a matrix which
contain zero.
For example.
m=matrix(c(1:4, c(0, 0, 0, 0), c(1, 0, 1, 1)), nc=4, byrow=TRUE)
I want to be able to calculate means for non-zero elements for each row.
## what i have is this
apply((m[apply(m != 0, MARGIN=1, any),]), 1, mean)
## which returns
[1]? 2.50? 0.75
### what i want to get returned is this
[1] 2.50? *1
*meaning that the last row calculated the mean ignoring the zero. Anyone
got any ideas?
??? [[alternative HTML version deleted]]
______________________________________________
R-help at 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.