I am a beginner to R programming and am trying to add one extra column to a matrix having 50 columns. This new column would be the avg of first 10 values in that row.
Methinks you are over thinking this.
a <- matrix(1:5000, nrow=100) a <- transform(a, first10ave = colMeans(a[1:10,]))
Bam!
a <- matrix(1:5000, nrow=100) a <- cbind(a,apply(a[,1:10],1,mean))
On big datasets it is however faster (and arguably simpler) to use:
cbind(a, rowMeans(a[,1:10]) )