[R] to raise in a loop more than 1

Rau, Roland Rau at demogr.mpg.de
Fri Jun 26 14:11:10 CEST 2009


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.




More information about the R-help mailing list