Skip to content
Prev 180582 / 398525 Next

Help with loops(corrected question)

Amit Patel wrote:
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