# [R] error in R

Paul Hiemstra p.hiemstra at geo.uu.nl
Wed Mar 3 14:36:46 CET 2010

```Hi Frederik,

There is no need for the double for loop:

b[,5] <- sin(runif(5,0,2*pi))

As to your question, check the values i and k take. In the first
iteration of the second loop k == 0, and R does not support an index
equal to 0. The problem is in 1:n-1, this gives 0- 4, in stead do
1:(n-1). Better is to skip the double for loop altogether and vectorization.

cheers,
Paul

frederik vanhaelst wrote:
> Hi,
>
> I want put some values in the last column of  a matrix b. But every time
> again there comes the same error on the screen...
>
>
>> b <- array(0, c(5,5))
>> m<-matrix(runif(20,0,2*pi),5)         # the sinus of this kind of values i
>>
> want put in the last column of b, m is a 5*4 matrix
>
>> n<-5
>> for(i in 1:n){
>>
> + a2<-1
> + for(k in 1: n-1){
> + a2<-a2*sin(m[i,k])
> + }
> + b[i,n]<-a2
> + }
> Error in b[i, n] <- a2 : replacement has length zero
> Is there someone who see the problem?
>
> Thanks a lot,
>
> Frederik
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul

```