Quantcast

Row means of matrix.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Row means of matrix.

CrimMagic
 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Row means of matrix.

David Winsemius

On Aug 17, 2012, at 8:28 PM, Yingwei Lee wrote:

> m=matrix(c(1:4, c(0, 0, 0, 0), c(1, 0, 1, 1)), nc=4, byrow=TRUE)

is.na(m) <- m==0
rowMeans(m, na.rm=T)
#[1] 2.5 NaN 1.0

--
David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Row means of matrix.

Berend Hasselman
In reply to this post by CrimMagic

On 18-08-2012, at 05:28, Yingwei Lee wrote:

> 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?

Define function to compute mean of non zero elements

 f <- function(x) {k <- which(x!=0); mean(x[k])}

then use apply

apply(A,1,f)
[1] 2.5 NaN 1.0

and to remove the NaN you could do

z <- apply(A,1,f)
z[!is.na(z)]

or

z[is.finite(z)]

Berend

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Row means of matrix.

arun kirshna
In reply to this post by CrimMagic
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 <[hidden email]>
To: [hidden email]
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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Row means of matrix.

CrimMagic
This post has NOT been accepted by the mailing list yet.
Thankyou all of you for your responses. I am able to make a working function now :D
Much appreciated!
Loading...