Ngayee.Law@celeradiagnostics.com
2002-Jun-14 17:16 UTC
[R] combination of different vector values
Hi, Thanks in advance for the help. I have a few vectors and I need to get all the combinations of the vector values. Normally I would use a few for-loops to do that. e.g x1_c(1,3,5) x2_c(2,5,6) for (i in x1) { for (j in x2) { ... }} My problem is that I don't know how many vectors there are ahead of time, so I don't know how many for-loops I need. Any ideas how to implement this? - Jacqueline -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Take a look at expand.grid. It sounds like it might solve your problem. Hope this helps, Matt -----Original Message----- From: Ngayee.Law at celeradiagnostics.com [mailto:Ngayee.Law at celeradiagnostics.com] Sent: Friday, June 14, 2002 1:16 PM To: r-help at stat.math.ethz.ch Subject: [R] combination of different vector values Hi, Thanks in advance for the help. I have a few vectors and I need to get all the combinations of the vector values. Normally I would use a few for-loops to do that. e.g x1_c(1,3,5) x2_c(2,5,6) for (i in x1) { for (j in x2) { ... }} My problem is that I don't know how many vectors there are ahead of time, so I don't know how many for-loops I need. Any ideas how to implement this? - Jacqueline -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Jacqueline, At 10:16 AM 6/14/2002 -0700, Ngayee.Law at celeradiagnostics.com wrote:>I have a few vectors and I need to get all the combinations of the vector >values. >Normally I would use a few for-loops to do that. >e.g x1_c(1,3,5) > x2_c(2,5,6) > for (i in x1) { > for (j in x2) { > ... > }} > >My problem is that I don't know how many vectors there are ahead of time, >so >I don't know how many for-loops I need. Any ideas how to implement this?You don't say what you want to do within the loops, but you may be able to adapt the following example, which puts the vectors in a list, loops over the list, and uses outer to generate combinations: > x <- 1:3 > y <- 10*(1:3) > z <- 100*(1:3) > lst <- list(x, y, z) > result <- lst[[1]] > for (i in 2:length(lst)) result <- outer(result, lst[[i]], '+') > as.vector(result) [1] 111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 [17] 232 233 311 312 313 321 322 323 331 332 333 If this doesn't work, perhaps you could specify the problem in more detail. John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._