# [R] Inserting a new row in a matrix

jim holtman jholtman at gmail.com
Tue Oct 21 21:41:06 CEST 2008

```What is 'insertRow'?  Is it returning a value?  It must be since you
can not change the value of parameters.  Maybe you need:

Ave <- insertRow(Avg,i,colMeans(res))

On Tue, Oct 21, 2008 at 2:19 PM, Alex99 <loyola9988 at yahoo.com> wrote:
>
> Hi guys,
>
> I need to insert a row to a matrix in a for loop. I have matrix named "Avg"
> which is a 5x4 matrix of zeros.
> I have a file named "A"(4 rows,14 columns) which I make a sample of it 5
> times. each time I get the mean for each column and put the result in the
> "Avg" matrix. this is my code:
>
> Avg=matrix(0,5,4)
>>
>>  for(i in 1:5){
> +  res=(A[sample(nrow(A), 5),])
> +  insertRow(Avg,i,colMeans(res))
> +  show(res)
> +  show(Avg)
> +  }
>
> this is my result(I just copied the first sample) :
>
>    s1 s2 s3 s4
> 2    1  0  1  0
> 3    0  0  0  0
> 11  0  0  0  0
> 5    0  1  0  1
> 12  0  0  0  0
>     [,1] [,2] [,3] [,4]
> [1,]    0    0    0    0
> [2,]    0    0    0    0
> [3,]    0    0    0    0
> [4,]    0    0    0    0
> [5,]    0    0    0    0
>
> it's suppose to put .2 .2 .2 .2 in the first row of the matrix but as you
> see it's all 0. any idea why and how to fix it?
>
> Thanks,
>
> --
> View this message in context: http://www.nabble.com/Inserting-a-new-row-in-a-matrix-tp20096320p20096320.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

```