Amit Patel wrote:
--- On Fri, 15/5/09, Amit Patel <amitrh...@yahoo.co.uk> wrote:
From: Amit Patel <amitrh...@yahoo.co.uk>
Subject: Help with loops
To: r-help@r-project.org
Date: Friday, 15 May, 2009, 12:17 PM
Hi
I am trying to create a loop which averages replicates in
my data.
The original data has many rows. and consists of 40 column
zz[,2:41] plus row headings in zz[,1]
I am trying to average each set of values (i.e. zz[1,2:3]
averaged and placed in average_value[1,2] and so on.
below is my script but it seems to be stuck in an endless
loop
Any suggestions??
If you want row-wise means, rowMeans does it without any loop, but
instead you could loop over the column indices or go without any
explicit loop as in:
averaged_value <- zz[,1:21]
temp <- as.matrix(zz[,2:41])
dim(temp) <- c(nrow(temp),2,20)
# and then either:
temp <- aperm(temp, c(1,3,2))
averaged_value[,2:21] <- rowMeans(temp, dim=2)
#
averaged_value[,2:21] <- apply(temp, c(1,3), mean)
Uwe Ligges
for (i in 1:length(zz[,1])) {
#calculates Meanss
#Sample A
average_value[i,2] <- rowMeans(zz[i,2:3])
average_value[i,3] <- rowMeans(zz[i,4:5])
average_value[i,4] <- rowMeans(zz[i,6:7])
average_value[i,5] <- rowMeans(zz[i,8:9])
average_value[i,6] <- rowMeans(zz[i,10:11])
#Sample B
average_value[i,7] <- rowMeans(zz[i,12:13])
average_value[i,8] <- rowMeans(zz[i,14:15])
average_value[i,9] <- rowMeans(zz[i,16:17])
average_value[i,10] <- rowMeans(zz[i,18:19])
average_value[i,11] <- rowMeans(zz[i,20:21])
#Sample C
average_value[i,12] <- rowMeans(zz[i,22:23])
average_value[i,13] <- rowMeans(zz[i,24:25])
average_value[i,14] <- rowMeans(zz[i,26:27])
average_value[i,15] <- rowMeans(zz[i,28:29])
average_value[i,16] <- rowMeans(zz[i,30:31])
#Sample D
average_value[i,17] <- rowMeans(zz[i,32:33])
average_value[i,18] <- rowMeans(zz[i,34:35])
average_value[i,19] <- rowMeans(zz[i,36:37])
average_value[i,20] <- rowMeans(zz[i,38:39])
average_value[i,21] <- rowMeans(zz[i,40:41])
}
thanks
______________________________________________
R-help@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.
______________________________________________
R-help@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.