[R] newbie question: ROW average
A Ezhil
ezhil02 at yahoo.com
Mon May 29 14:58:36 CEST 2006
Hi,
Thank you all (Dimitris, Peter, Rolf, Gabor) for your
suggestions. I tried with all your suggestions. I am
getting different answers when I use:
rowsum(mat, gl(nrow(mat)/4, 4)) / 4
and
m.new <-
t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))
When I tried with (assuming 'M' is my old matrix):
dim(M) <- c(4,248,74)
mn <- apply(M, c(2,3), mean)
the following error occured:
Error: dim<- : dims [product 73408] do not match the
length of object [74]
When I manually checked the answers, it seems that
rowsum(mat, gl(nrow(mat)/4, 4)) / 4 gives me the
correct answer.
Thanks again for your time & suggestions.
Regards,
Ezhil
--- Dimitris Rizopoulos
<dimitris.rizopoulos at med.kuleuven.be> wrote:
> yes you're right; it was my mistake.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
>
>
http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
> ----- Original Message -----
> From: "Rolf Turner" <rolf at math.unb.ca>
> To: <dimitris.rizopoulos at med.kuleuven.be>;
> <ezhil02 at yahoo.com>
> Cc: <r-help at stat.math.ethz.ch>
> Sent: Monday, May 29, 2006 1:55 PM
> Subject: Re: [R] newbie question: ROW average
>
>
> > Dimitris Rizopoulos wrote:
> >
> >> look at ?rowMeans; you can also use "apply(mat,
> 1, mean)" but
> >> rowMeans() is better.
> >
> > By my reading of the question, this is not what
> > Ezhil wants. He said:
> >
> > ``I have a 992 x 74 matrix. I would like to form a
> new matrix
> > by averaging each 4 rows from the original
> one.''
> >
> > I.e. he wants (I think) the first row of the new
> matrix
> > to be the mean of the first 4 rows of the old one,
> the
> > second row of the new matrix to be the mean of
> rows 5
> > through 8 of the old one, and so on.
> >
> > One way this could be done is via
> >
> > > m.new <-
>
t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))
> >
> > cheers,
> >
> > Rolf Turner
> > rolf at math.unb.ca
> >
>
> Disclaimer:
> http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>
More information about the R-help
mailing list