error with seq() used in for loop
On 11/10/2015 7:52 AM, Maram SAlem 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.
The range of your loop is 1:m-1, where m is 4. That is
m <- 4 1:m-1
[1] 0 1 2 3 and x[0] is length 0. I think you wanted 1:(m-1) (or even better, seq_len(m-1)) for your loop values. Duncan Murdoch
Thanks for helping. Maram [[alternative HTML version deleted]]
______________________________________________ R-help at 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.