Hi, I'm just coming to grips with "for" looping etc. and have a bit of a problem: I want to generate a sequence which goes 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... i.e. 8 consecutive numbers then 5 missed then the next 8 numbers etc. I was going to do this using the seq() function but couldn't figure out how so I thought I'd try a loop: for (x in seq(1,650,13)) { num.set.1 <- x:x+8 } but now what I need to do is write code such that each time it goes through the loop it assigns the output to a different object e.g. num.set.1 on the first loop then num.set.2 on the next etc. so that they can be concatenated. Is there any way to do this?? I may be doing this an extremely complicated way but with my zero programming experience its the best I can think of. Can anyone help? J
Your sequence is 1:a + k*(a+b) where a=8, b=5 and k=0,1,...,K. You can make use of the fact that R loops of vectors if two vectors are not the same; a <- 8 b <- 5 K <- 49 x <- rep((0:K)*(a+b), each=a) + 1:a Cheers Henrik Bengtsson> -----Original Message----- > From: r-help-admin at stat.math.ethz.ch > [mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Jeremy Z Butler > Sent: den 4 mars 2003 14:14 > To: r-help at stat.math.ethz.ch > Subject: [R] for loop problem > > > Hi, > I'm just coming to grips with "for" looping etc. and have a bit of a > problem: > > I want to generate a sequence which goes > 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... > i.e. 8 consecutive numbers then 5 missed then the next 8 > numbers etc. I was going to do this using the seq() function > but couldn't figure out how > so I thought I'd try a loop: > > for (x in seq(1,650,13)) > { num.set.1 <- x:x+8 > } > but now what I need to do is write code such that each time > it goes through > the loop it assigns the output to a different object e.g. > num.set.1 on the > first loop then num.set.2 on the next etc. so that they can > be concatenated. > Is there any way to do this?? > > I may be doing this an extremely complicated way but with my zero > programming experience its the best I can think of. Can anyone help? > > J > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/> r-help > >
"Jeremy Z Butler" <jerrytheshrub at hotmail.com> writes:> Hi, > I'm just coming to grips with "for" looping etc. and have a bit of a > problem: > > > I want to generate a sequence which goes > 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... > i.e. 8 consecutive numbers then 5 missed then the next 8 numbers etc. > I was going to do this using the seq() function but couldn't figure > out how so I thought I'd try a loop: > > > for (x in seq(1,650,13)) > { num.set.1 <- x:x+8 > } > but now what I need to do is write code such that each time it goes > through the loop it assigns the output to a different object > e.g. num.set.1 on the first loop then num.set.2 on the next etc. so > that they can be concatenated. Is there any way to do this?? > > > I may be doing this an extremely complicated way but with my zero > programming experience its the best I can think of. Can anyone help?I suggest using a matrix.> mseq = as.vector(matrix(1:650, nrow = 13)[1:8,]) > length(mseq)[1] 400> mseq[1:20][1] 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 27 28 29 30
On Tue, 04 Mar 2003, Jeremy Z. Butler told this:> I want to generate a sequence which goes 1 2 3 4 5 6 7 8 14 15 16 17 18 19 > 20 21 26 27 ... i.e. 8 consecutive numbers then 5 missed then the next 8 > numbers etc. I was going to do this using the seq() function but couldn't > figure out how so I thought I'd try a loop: > > for (x in seq(1,650,13)) > { num.set.1 <- x:x+8^^^^^ should be x:(x+8)> }and by all means, avoid for loop, think in vector. Michael