[R] How to create arbitrary number of loops in R
Dai, Hongying,
hdai at cmh.edu
Fri Mar 30 03:40:58 CEST 2012
I figured out how to convert the array to a vector.
library(irr)
M <- matrix(1:50,nc=10)
unlist(combn(10, 2, function(v) icc(M[,v]))[7,])
This code will get all I need. Please ignore my question below. Thanks!
Daisy
-----Original Message-----
From: Dai, Hongying,
Sent: Thursday, March 29, 2012 4:58 PM
To: r-help at 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 icc value vector. Under the array format, how can I extract icc value from the array output and turn it into a vector?
#ilai's code:
library(irr)
M <- matrix(1:50,nc=10)
c2way <- combn(ncol(M),2)
MM <- M[,c2way]
dim(MM) <- c(nrow(M),nrow(c2way),ncol(c2way))
MM
A<-apply(MM,3,icc) #How can I combine A[[1]]$value, A[[2]]$value, ... into a vector?
#Christian's code:
library(irr)
M <- matrix(1:50,nc=10)
combn(10, 2, function(v) icc(M[,v])) #How can I get all values in row 7 as a vector?
Thanks!
Daisy
-----Original Message-----
From: ilaik9 at gmail.com [mailto:ilaik9 at gmail.com] On Behalf Of ilai
Sent: Thursday, March 29, 2012 10:46 AM
To: Dai, Hongying,
Cc: r-help at 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 <keren at 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 return icc(xx) in f
Cheers
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.
More information about the R-help
mailing list