Dear all! After hours of trying around, I gave up: I have a 2-dimensional array, and I know how to split it into its rows and how to get the mean for every row using 'sapply'. But what I want is to calculate the mean over the first n rows, and then the second n rows, etc., so that I get a vector like: v == mean1(row 1:5), mean2(row6:10),... (trivial, you might say. I find it rather mind-boggling, though: I tried to get the mean from the array before splitting it, after splitting it, looping through it with for-loops...I feel like an idiot by now; looks like I missed a crucial point of how 'R' works.) Thanks a lot in advance! -- ______________________________________________________ Jan Wantia Dept. of Information Technology, University of Z?rich Andreasstr. 15 CH 8050 Z?rich Switzerland Tel.: +41 (0) 1 635 4315 Fax: +41 (0) 1 635 45 07 email: wantia at ifi.unizh.ch
1. Using rowMeans() is more efficient for computing row means. 2. "Mean of five rows" is the same as "mean of the five row means" (exception: if you have NAs, and set na.rm=TRUE). So you can just do something like: k <- ceiling(nrow(x) / 5) ktimes <- if (rem <- nrow(x) %% 5) c(rep(5, k-1), rem) else rep(5, k) mean5row <- tapply(rowMeans(x), rep(1:k, ktimes)) (This is untested!) HTH, Andy> From: Jan Wantia > > Dear all! > > After hours of trying around, I gave up: > > I have a 2-dimensional array, and I know how to split it into > its rows > and how to get the mean for every row using 'sapply'. > But what I want is to calculate the mean over the first n > rows, and then > the second n rows, etc., so that I get a vector like: > > v == mean1(row 1:5), mean2(row6:10),... > > (trivial, you might say. I find it rather mind-boggling, > though: I tried > to get the mean from the array before splitting it, after > splitting it, > looping through it with for-loops...I feel like an idiot by > now; looks > like I missed a crucial point of how 'R' works.) > > Thanks a lot in advance! > -- > > ______________________________________________________ > > Jan Wantia > Dept. of Information Technology, University of Z?rich > Andreasstr. 15 > CH 8050 Z?rich > Switzerland > > Tel.: +41 (0) 1 635 4315 > Fax: +41 (0) 1 635 45 07 > email: wantia at ifi.unizh.ch
Take a look at tapply and %/% which could help you to create indices for groups. > x=cbind(id=1:23,var1=rnorm(23)) > tapply(x[,"var1"],1:length(x[,"var1"])%/%5,rowMeans) 0 1 2 3 4 0.393473633 0.412297253 -0.221925003 -0.005212217 -1.564881727 If your data has severall variables, also look at aggregate > x=cbind(var1=rnorm(23),var2=rnorm(23)) > aggregate(x,by=list(groups=1:dim(x)[1]%/%5),FUN=mean) (and then you can call rowMeans on the result) Eric At 14:03 4/12/2003, Jan Wantia wrote:>Dear all! > >After hours of trying around, I gave up: > >I have a 2-dimensional array, and I know how to split it into its rows and >how to get the mean for every row using 'sapply'. >But what I want is to calculate the mean over the first n rows, and then >the second n rows, etc., so that I get a vector like: > >v == mean1(row 1:5), mean2(row6:10),... > >(trivial, you might say. I find it rather mind-boggling, though: I tried >to get the mean from the array before splitting it, after splitting it, >looping through it with for-loops...I feel like an idiot by now; looks >like I missed a crucial point of how 'R' works.) > >Thanks a lot in advance! >-- > >______________________________________________________ > >Jan Wantia >Dept. of Information Technology, University of Z?rich >Andreasstr. 15 >CH 8050 Z?rich >Switzerland > >Tel.: +41 (0) 1 635 4315 >Fax: +41 (0) 1 635 45 07 >email: wantia at ifi.unizh.ch > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help-------------------------------------------------- L'erreur est certes humaine, mais un vrai d?sastre n?cessite un ou deux ordinateurs. Citation anonyme -------------------------------------------------- Eric Lecoutre Informaticien/Statisticien Institut de Statistique / UCL TEL (+32)(0)10473050 lecoutre at stat.ucl.ac.be URL http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
The following: c( tapply( x, (row(x)-1)%/%5, mean ) ) gives a vector whose first element is the mean of every element in the rows 1 through 5 inclusive, whose second element is the mean of every element rows 6 through 10 inclusive, etc. --- Date: 4 Dec 2003 14:03:57 +0100 From: Jan Wantia <wantia at ifi.unizh.ch> To: <r-help at stat.math.ethz.ch> Subject: [R] get mean of several rows Dear all! After hours of trying around, I gave up: I have a 2-dimensional array, and I know how to split it into its rows and how to get the mean for every row using 'sapply'. But what I want is to calculate the mean over the first n rows, and then the second n rows, etc., so that I get a vector like: v == mean1(row 1:5), mean2(row6:10),... (trivial, you might say. I find it rather mind-boggling, though: I tried to get the mean from the array before splitting it, after splitting it, looping through it with for-loops...I feel like an idiot by now; looks like I missed a crucial point of how 'R' works.) Thanks a lot in advance! -- ______________________________________________________ Jan Wantia Dept. of Information Technology, University of Zürich Andreasstr. 15 CH 8050 Zürich Switzerland
On Thu, 4 Dec 2003, Jan Wantia wrote:> Dear all! > > After hours of trying around, I gave up: > > I have a 2-dimensional array, and I know how to split it into its rows > and how to get the mean for every row using 'sapply'. > But what I want is to calculate the mean over the first n rows, and then > the second n rows, etc., so that I get a vector like: > > v == mean1(row 1:5), mean2(row6:10),... > > (trivial, you might say. I find it rather mind-boggling, though: I tried > to get the mean from the array before splitting it, after splitting it, > looping through it with for-loops...I feel like an idiot by now; looks > like I missed a crucial point of how 'R' works.) >There is also a function rowsum() for doing precisely this (it's quite a bit faster than the tapply solution). -thomas
"Jan Wantia" <wantia at ifi.unizh.ch> asked: I have a 2-dimensional array, and I know how to split it into its rows and how to get the mean for every row using 'sapply'. But what I want is to calculate the mean over the first n rows, and then the second n rows, etc., so that I get a vector like: v == mean1(row 1:5), mean2(row6:10),... (trivial, you might say. There are lots of ways to do it. Let Arr be a p=nrow(Arr) by q=ncol(Arr) array. Let p %% n = 0, for simplicity. Let 1 <= k <= p %/% n. Then the rows which should contribute to the kth mean are (k-1)*n+1 .. k*n. mean(Arr[((k-1)*n+1):(k*n)]) will therefore give you the kth mean. So sapply(1:(nrow(Arr)%/%n), function (k) mean(Arr[((k-1)*n+1):(k*n),])) should do the trick. I've tested it on a small example where I knew the answers, and it worked. This assumes that I've understood the question... I'm a bit annoyed, because I thought of several really cute ways to do this, one involving cumsum(t(Arr)) and diff(), but this is so direct that the others might only be confusing.
Dear all! Thanks to all who replied to my question on getting the means of several rows, and the one with the standard error + mean-plot! Many of them worked fine just as they were, others had to be adapted a bit. However, I can finally do my calculations, and find myself happy as a man could be, plotting fancy graphs whole day long. Moreover, I think I have learned quite a bit on R, seeing so many ways to do the same thing. Thanks a lot, again, I was really at the edge of going home and get drunk! Cheers, Jan -- ______________________________________________________ Jan Wantia Dept. of Information Technology, University of Z?rich Andreasstr. 15 CH 8050 Z?rich Switzerland Tel.: +41 (0) 1 635 4315 Fax: +41 (0) 1 635 45 07 email: wantia at ifi.unizh.ch