Hi,
I am new to R programming. I have a 992 x 74 matrix. I
would like to form a new matrix by averging each 4
rows
from the original one. How can I use 'apply' instead
of usual mean inside the nested for loop?
Thanks in advance.
regards,
ezhil
__
/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
- Original Message -
From: A Ezhil [EMAIL PROTECTED]
To: r-help r-help@stat.math.ethz.ch
Sent: Monday, May 29, 2006 1:24 PM
Subject: [R] newbie question: ROW average
Hi,
I am new
A Ezhil [EMAIL PROTECTED] writes:
Hi,
I am new to R programming. I have a 992 x 74 matrix. I
would like to form a new matrix by averging each 4
rows
from the original one. How can I use 'apply' instead
of usual mean inside the nested for loop?
How about
dim(M) - c(4,248,74)
mn -
Peter Dalgaard wrote:
How about
dim(M) - c(4,248,74)
mn - apply(M, c(2,3), mean)
Hey! That's sexy! Much better than my kludgy
suggestion!
cheers,
Rolf
__
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
/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
- Original Message -
From: Rolf Turner [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Cc: r-help@stat.math.ethz.ch
Sent: Monday, May 29, 2006 1:55 PM
Subject: Re: [R] newbie question: ROW average
Dimitris Rizopoulos
Try this:
rowsum(mat, gl(nrow(mat)/4, 4)) / 4
On 5/29/06, A Ezhil [EMAIL PROTECTED] wrote:
Hi,
I am new to R programming. I have a 992 x 74 matrix. I
would like to form a new matrix by averging each 4
rows
from the original one. How can I use 'apply' instead
of usual mean inside the
@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
A Ezhil [EMAIL PROTECTED] writes:
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]
In that case, M clearly wasn't a 992x74 matrix!