[R] error with seq() used in for loop

Maram SAlem marammagdysalem at gmail.com
Sun Oct 11 13:52:42 CEST 2015


Dear All,
I have a question concerning the seq() function arguments when used in a
for() loop.
I'll simplify this question to make it more clear. Suppose I have a
(5*3)matrix s (for ex.) and I need to write a function with  for() loop, in
each step of the loop I need to generate a sequence whose upper limit is
the ith element of the first row of s, then put the resulting sequences in
a list. I used the following simple code (I've only included the first part
of the function)


> s<-matrix(c(1,0,1,0,1,1,0,0,1,0,0,0,0,0,1),nrow=5,byrow=TRUE)
> simpfun<- function (x,n,m,p,alpha,beta)
+ {
+ LD<-list()
+ for (i in 1:m-1)
+ {
+ LD[[i]]<-seq(0,x[i],1)
+ }
+ print(LD)
+ }
> mk<-simpfun(s[1,],n=6,m=4,p=0.3)
Error in seq.default(0, x[i], 1) : 'to' must be of length 1

Although x is supposed to be the vector
1 0 1
and thus x[1]=1, x[2]=0,x[3]=1.
So I don't get why the error "Error in seq.default(0, x[i], 1) : 'to' must
be of length 1" occurs in the first place.

Thanks for helping.

Maram

	[[alternative HTML version deleted]]



More information about the R-help mailing list