[R] matrix data generation under 2 loops

arun smartpink111 at yahoo.com
Fri Jun 28 00:19:23 CEST 2013


Hi,
May be this helps:
res1<-as.matrix(transform(expand.grid(k1=0.3*(1:10),k2=1:10),k3=k1*k2))

A.K.


Dear all, 

I'm trying to generate a matrix dataset following 2 loops analysis, such as the follows: 

for (i in 1:10)  { 

k1 <- 0.3 x i 

for (j in 1:10)  { 

k2 <- j 

k3 <- k1*k2 

.... 

}} 

I would like to generate a dataset with each row containing k1, 
k2, and corresponding k3. As there's 100 combination of k1 and k2, so 
there would be 100 rows data.  how could I do it? 

Thanks for the help. 

York



More information about the R-help mailing list