[R] How to create arbitrary number of loops in R

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Mar 30 02:00:02 CEST 2012


It seems to me that if you looked at Christian's code carefully you might wonder what it means to omit a value for the row index, and then you might (re-)read "An Introduction To R" (that comes with R) or you might learn that "[" is a function that you can search for help on ( ?"[" ) where there are many examples, or you might try omitting an index value just to see what happens.
In other words, I would hope you weren't using the very short examples provided on this list without taking the opportunity to understand them, because that would be depressing and a growing number of people might feel it wasn't worth answering questions on the list any more.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

"Dai, Hongying," <hdai at cmh.edu> wrote:

>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.
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>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 mailing list