I am new to more radical programming in R. I am trying to write a nested 'for' loop to produce output that takes subscripts like: for i taking values 1,2,3,4,5 and j taking values 1,2,3 I want to output for a computation using the combination values of i and j a value x like this; i j x 1 1 x11 1 2 x12 1 3 x13 2 1 x21 2 2 x22 2 3 x23 3 1 x31 3 2 x32 3 3 x33 ........ ....... Need help urgently. Thanks. Philip A Smile costs Nothing But Rewards Everything Happiness is not perfected until it is shared -Jane Porter [[alternative HTML version deleted]]
Try: paste("x", as.vector(t(outer(1:4, 1:3, FUN=paste, sep=""))), sep="") or paste("x", apply(expand.grid(1:4, 1:3), 1, paste, collapse=""), sep="") On Mon, May 19, 2008 at 4:40 PM, Philip Twumasi-Ankrah < nana_kwadwo_derkyi@yahoo.com> wrote:> I am new to more radical programming in R. I am trying to write a nested > 'for' loop to produce output that takes subscripts like: > > for i taking values 1,2,3,4,5 and > j taking values 1,2,3 > > I want to output for a computation using the combination values of i and j > a value x like this; > > i j x > 1 1 x11 > 1 2 x12 > 1 3 x13 > 2 1 x21 > 2 2 x22 > 2 3 x23 > 3 1 x31 > 3 2 x32 > 3 3 x33 > ........ > ....... > > Need help urgently. > > Thanks. > > Philip > > > A Smile costs Nothing > > But Rewards Everything > > Happiness is not perfected until it is shared > -Jane Porter > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hey Philip, I'm not sure if I understand what your "x11", "x12", etc. are. You can combine the values of your two vectors using the expand.grid function. There is no need to do nester FOR loops: > i=c(1,2,3,4,5) > j=c(1,2,3) > x=expand.grid(i,j) > print (x) Var1 Var2 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 1 2 7 2 2 8 3 2 9 4 2 10 5 2 11 1 3 12 2 3 13 3 3 14 4 3 15 5 3 Hope this helps, Julian Philip Twumasi-Ankrah wrote:> I am new to more radical programming in R. I am trying to write a nested 'for' loop to produce output that takes subscripts like: > > for i taking values 1,2,3,4,5 and > j taking values 1,2,3 > > I want to output for a computation using the combination values of i and j a value x like this; > > i j x > 1 1 x11 > 1 2 x12 > 1 3 x13 > 2 1 x21 > 2 2 x22 > 2 3 x23 > 3 1 x31 > 3 2 x32 > 3 3 x33 > ........ > ....... > > Need help urgently. > > Thanks. > > Philip > > > A Smile costs Nothing > > But Rewards Everything > > Happiness is not perfected until it is shared > -Jane Porter > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code.