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
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))
MM
c3way - combn(ncol(M),3)
MMM - M[,c3way]
dim(MMM) - c(nrow(M),nrow(c3way),ncol(c3way))
MMM
etc. etc.
Than you can (untested for icc) loop your
On Thu, Mar 29, 2012 at 9:27 AM, ilai ke...@math.montana.edu wrote:
Oops, sent to fast. A (maybe) clearer solution:
f - function(x,m){
cmway - combn(ncol(x),m)
xx - x[,cmway]
dim(xx) - c(nrow(x),nrow(cmway),ncol(cmway))
xx
}
f(M,3)
str(sapply(2:4,f,x=M))
And again lapply / apply , or even
: [R] How to create arbitrary number of loops in R
On Thu, Mar 29, 2012 at 9:27 AM, ilai ke...@math.montana.edu wrote:
Oops, sent to fast. A (maybe) clearer solution:
f - function(x,m){
cmway - combn(ncol(x),m)
xx - x[,cmway]
dim(xx) - c(nrow(x),nrow(cmway),ncol(cmway))
xx
}
f(M,3)
str(sapply(2
:46 AM
To: Dai, Hongying,
Cc: r-help@r-project.org
Subject: Re: [R] How to create arbitrary number of loops in R
On Thu, Mar 29, 2012 at 9:27 AM, ilai ke...@math.montana.edu wrote:
Oops, sent to fast. A (maybe) clearer solution:
f - function(x,m){
cmway - combn(ncol(x),m)
xx - x[,cmway]
dim(xx
:58 PM
To: r-help@r-project.org
Subject: RE: [R] How to create arbitrary number of loops in R
Thanks, ilai and Christian! Both codes are working but I have one more question.
Both codes return results in array format. In my original code, I get icc value
from icc(data)$value and use loop to get
6 matches
Mail list logo