I would raise x,y and z in a loop but I won't raise of 1. I tried this but it doesn't work mydata=matrix(nrow=1500,ncol=3) i=1 for(x in 0:10){ for(y in 0:20){ for(z in 0:10){ mydata[i,]=c(x,y,z) i=i+1 z=z+2} y=y+4} x=x+2} And I would have something like that x y z 0 0 0 0 0 2 0 0 4 0 0 6 0 0 8 0 0 10 0 4 0 0 4 2 ... Could anybody help me? I don't work on a special package to do it... Thanks Cordialement Damien Landais
Hi does expand.grid(z=seq(0,6,2), y=seq(0,8,4), x=seq(0,10,2)) do what you want? Petr r-help-bounces at r-project.org napsal dne 26.06.2009 11:15:42:> I would raise x,y and z in a loop but I won't raise of 1. I tried thisbut it> doesn't work > > mydata=matrix(nrow=1500,ncol=3) > i=1 > for(x in 0:10){ > for(y in 0:20){ > for(z in 0:10){ > mydata[i,]=c(x,y,z) > i=i+1 > z=z+2} > y=y+4} > x=x+2} > > And I would have something like that > x y z > 0 0 0 > 0 0 2 > 0 0 4 > 0 0 6 > 0 0 8 > 0 0 10 > 0 4 0 > 0 4 2 > ... > > Could anybody help me? > I don't work on a special package to do it... > > Thanks > > Cordialement > Damien Landais > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Dear Damien,> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of damien landais > Sent: Friday, June 26, 2009 11:16 AM > To: R-help at r-project.org > Subject: [R] to raise in a loop more than 1 > > I would raise x,y and z in a loop but I won't raise of 1. I > tried this but it doesn't work > > mydata=matrix(nrow=1500,ncol=3) > i=1 > for(x in 0:10){ > for(y in 0:20){ > for(z in 0:10){ > mydata[i,]=c(x,y,z) > i=i+1 > z=z+2} > y=y+4} > x=x+2} > > And I would have something like that > x y z > 0 0 0 > 0 0 2 > 0 0 4 > 0 0 6 > 0 0 8 > 0 0 10 > 0 4 0 > 0 4 2 > ... >I am not exactly sure what you want. If my guess is correct, this will reproduce the desired output: x <- seq(from=0, to=10, by=2) y <- seq(from=0, to=20, by=4) z <- seq(from=0, to=10, by=2) mydata2 <- expand.grid(x=as.factor(x), y=as.factor(y), z=as.factor(z)) mydata3 <- with(mydata2, mydata2[order(x,y,z),] ) head(mydata3, n=8) Hope this helps, Roland ---------- This mail has been sent through the MPI for Demographic Research. Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.
I assume that you are getting this error:> mydata=matrix(nrow=1500,ncol=3) > i=1 > for(x in 0:10){+ for(y in 0:20){ + for(z in 0:10){ + mydata[i,]=c(x,y,z) + i=i+1 + z=z+2} + y=y+4} + x=x+2} Error in mydata[i, ] = c(x, y, z) : subscript out of bounds No suitable frames for recover()> i[1] 1501>You have indexed outside the limits. For what you are doing, you need the matrix to be 11*21*11=2451> mydata=matrix(nrow=2541,ncol=3) > i=1 > for(x in 0:10){+ for(y in 0:20){ + for(z in 0:10){ + mydata[i,]=c(x,y,z) + i=i+1 + z=z+2} + y=y+4} + x=x+2}> > head(mydata)[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 1 [3,] 0 0 2 [4,] 0 0 3 [5,] 0 0 4 [6,] 0 0 5>You still don't get what you are expecting because you are trying to change the loop variable within the loop which is not allowed (or really ignored). On Fri, Jun 26, 2009 at 5:15 AM, damien landais <damien.landais@tdf.fr>wrote:> I would raise x,y and z in a loop but I won't raise of 1. I tried this but > it doesn't work > > mydata=matrix(nrow=1500,ncol=3) > i=1 > for(x in 0:10){ > for(y in 0:20){ > for(z in 0:10){ > mydata[i,]=c(x,y,z) > i=i+1 > z=z+2} > y=y+4} > x=x+2} > > And I would have something like that > x y z > 0 0 0 > 0 0 2 > 0 0 4 > 0 0 6 > 0 0 8 > 0 0 10 > 0 4 0 > 0 4 2 > ... > > Could anybody help me? > I don't work on a special package to do it... > > Thanks > > Cordialement > Damien Landais > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]