[R] How to create arbitrary number of loops in R
keren at math.montana.edu
Thu Mar 29 17:27:34 CEST 2012
To cbind, you don't need a loop
(M <- matrix(1:50,nc=10))
c2way <- combn(ncol(M),2)
MM <- M[,c2way]
dim(MM) <- c(nrow(M),nrow(c2way),ncol(c2way))
c3way <- combn(ncol(M),3)
MMM <- M[,c3way]
dim(MMM) <- c(nrow(M),nrow(c3way),ncol(c3way))
Than you can (untested for icc) loop your function as in
On Thu, Mar 29, 2012 at 7:28 AM, 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
> for (i in 1:(n-1))
> for (j in (i+1):n)
> If I need three-way combination, then a code with three "for" loops will be:
> for (i in 1:(n-2))
> for (j in (i+1):(n-1))
> for (k in (j+1):n)
> But how can I write a code if I need all m=2, 3, 4,... loops for arbitrary m-way combinations?
> 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 informationsecurityofficer at cmh.edu and expunge this communication without making any copies. Thank you for your cooperation.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help