Thanks a lot for your help and patience Duncan. It seems that my questions is really a trivial one but I never realized that 1:4 means 0 1 2 3 4 , never knew it starts from 0. On 11 October 2015 at 14:07, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> 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. > > > >[[alternative HTML version deleted]]
> On 11 Oct 2015, at 14:12, Maram SAlem <marammagdysalem at gmail.com> wrote: > > Thanks a lot for your help and patience Duncan. > It seems that my questions is really a trivial one but I never realized > that 1:4 means 0 1 2 3 4 , never knew it starts from 0. >It doesn?t mean what you mention. 1:4 is the sequence 1 2 3 4 1:4-1 is (1 2 3 4) -1 ==> 0 1 2 3 The 1 is subtracted from all elements of the sequence. Berend> > > On 11 October 2015 at 14:07, Duncan Murdoch <murdoch.duncan at gmail.com> > wrote: > >> 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. >>> >> >> > > [[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.
I got you Berend. Thanks again On 11 October 2015 at 14:20, Berend Hasselman <bhh at xs4all.nl> wrote:> > > On 11 Oct 2015, at 14:12, Maram SAlem <marammagdysalem at gmail.com> wrote: > > > > Thanks a lot for your help and patience Duncan. > > It seems that my questions is really a trivial one but I never realized > > that 1:4 means 0 1 2 3 4 , never knew it starts from 0. > > > > It doesn?t mean what you mention. > > 1:4 is the sequence 1 2 3 4 > > 1:4-1 is (1 2 3 4) -1 ==> 0 1 2 3 > > The 1 is subtracted from all elements of the sequence. > > Berend > > > > > > On 11 October 2015 at 14:07, Duncan Murdoch <murdoch.duncan at gmail.com> > > wrote: > > > >> 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. > >>> > >> > >> > > > > [[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. > >[[alternative HTML version deleted]]