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

From: Sarah Goslee <sarah.goslee_at_gmail.com>
Date: Thu, 29 Mar 2012 09:35:27 -0400

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, <hdai_at_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_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
```
Received on Thu 29 Mar 2012 - 13:54:31 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 29 Mar 2012 - 14:15:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.