[Rd] How to create arbitrary number of loops

2012-03-29 Thread Dai, Hongying,
Dear R users,

I'm wondering how I can generate an arbitrary number of loops in R.
For instance, I can generate two for loops to get ICC among any two-way 
combination among 10 variables. Here is the code

n-10
for (i in 1:(n-1))
{
for (j in (i+1):n)
{
icc(cbind(DATA[,i],DATA[,j]))
}
}
If I need three-way combination, then a code with three for loops will be:
n-10
for (i in 1:(n-2))
{
for (j in (i+1):(n-1))
{
for (k in (j+1):n)
{
icc(cbind(DATA[,i],DATA[,j],DATA[,k]))
}
}
}
But how can I write a code if I need all m=2, 3, 4,... loops for arbitrary 
m-way combinations?
Thanks!
Daisy


Electronic mail from Children's Mercy Hospitals and Clinics. This communication 
is intended only for the use of the addressee. It may contain information that 
is privileged or confidential under applicable law. If you are not the intended 
recipient or the agent of the recipient, you are hereby notified that any 
dissemination, copy or disclosure of this communication is strictly prohibited. 
If you have received this communication in error, please immediately forward 
the message to Children's Mercy Hospital's Information Security Officer via 
return electronic mail at informationsecurityoffi...@cmh.edu and expunge this 
communication without making any copies. Thank you for your cooperation.

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to create arbitrary number of loops

2012-03-29 Thread Sarah Goslee
That sounds like a job for recursion.

And also, a question for r-help and not r-devel.

Sarah

On Wed, Mar 28, 2012 at 5:34 PM, Dai, Hongying, h...@cmh.edu wrote:
 Dear R users,

 I'm wondering how I can generate an arbitrary number of loops in R.
 For instance, I can generate two for loops to get ICC among any two-way 
 combination among 10 variables. Here is the code

 n-10
 for (i in 1:(n-1))
 {
 for (j in (i+1):n)
 {
 icc(cbind(DATA[,i],DATA[,j]))
 }
 }
 If I need three-way combination, then a code with three for loops will be:
 n-10
 for (i in 1:(n-2))
 {
 for (j in (i+1):(n-1))
 {
 for (k in (j+1):n)
 {
 icc(cbind(DATA[,i],DATA[,j],DATA[,k]))
 }
 }
 }
 But how can I write a code if I need all m=2, 3, 4,... loops for arbitrary 
 m-way combinations?
 Thanks!
 Daisy

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to create arbitrary number of loops

2012-03-29 Thread Christian Brechbühler
On Wed, Mar 28, 2012 at 5:34 PM, Dai, Hongying, h...@cmh.edu wrote:

 Dear R users,

 I'm wondering how I can generate an arbitrary number of loops in R.
 For instance, I can generate two for loops to get ICC among any two-way
 combination among 10 variables. Here is the code

 n-10
 for (i in 1:(n-1))
 {
 for (j in (i+1):n)
 {
 icc(cbind(DATA[,i],DATA[,j]))
 }
 }
 If I need three-way combination, then a code with three for loops will
 be:
 n-10
 for (i in 1:(n-2))
 {
 for (j in (i+1):(n-1))
 {
 for (k in (j+1):n)
 {
 icc(cbind(DATA[,i],DATA[,j],DATA[,k]))
 }
 }
 }
 But how can I write a code if I need all m=2, 3, 4,... loops for arbitrary
 m-way combinations?
 Thanks!
 Daisy


For your specific specific case,

combn(10, m, function(v) icc(DATA[,v]))

My 'v' is your c(i,j) or c(i,j,k), etc.

In general, Sarah is right, recursion gives the neater code, and this
discussion probably doesn't belong here.

It can be achieved without recursion; I'd gladly share code that does the
equivalent of combn (which is what Daisy is asking for) both ways.  I wrote
it many years ago in C, and the non-recursive code is probably the only
time I used the 'goto' statement :-)

/Christian

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel