> On 11 Oct 2015, at 13:52, Maram SAlem <marammagdysa...@gmail.com> wrote: > > 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.
Use for (i in 1:(m-1)) in stead of what you put in the function. Read the section “Generating regular sequences” in the "An Introduction to R“ manual. Berend ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.