Hi there, I am looking for a function that takes a vector as input and generates all pair wise combination of the elements of the input vector. For example, the input vector is c(1,2,3,4). The output vector is c(1 and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4). The representation here is generic. I can use a for loop to do it, but it gets slow when the number of elements of the input vector is large. Best, Jia -- Ohio State University - Finance 248 Fisher Hall 2100 Neil Ave. Columbus, Ohio 43210 Telephone: 614-292-2830 http://www.fisher.osu.edu/~chen_1002/
try this: combn(c(1,2,3,4), 2) On Tue, May 11, 2010 at 2:45 PM, chen jia <chen_1002@fisher.osu.edu> wrote:> Hi there, > > I am looking for a function that takes a vector as input and generates > all pair wise combination of the elements of the input vector. > > For example, the input vector is c(1,2,3,4). The output vector is c(1 > and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4). The > representation here is generic. > > I can use a for loop to do it, but it gets slow when the number of > elements of the input vector is large. > > Best, > Jia > > -- > Ohio State University - Finance > 248 Fisher Hall > 2100 Neil Ave. > Columbus, Ohio 43210 > Telephone: 614-292-2830 > http://www.fisher.osu.edu/~chen_1002/<http://www.fisher.osu.edu/%7Echen_1002/> > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On May 11, 2010, at 1:45 PM, chen jia wrote:> Hi there, > > I am looking for a function that takes a vector as input and generates > all pair wise combination of the elements of the input vector.?combn> > For example, the input vector is c(1,2,3,4). The output vector is c(1 > and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4).That's not a vector.> The > representation here is generic. > > I can use a for loop to do it, but it gets slow when the number of > elements of the input vector is large. > > Best, > Jia >David Winsemius, MD West Hartford, CT
combn(1:4, 2) will do it. -Ista On Tuesday 11 May 2010 1:45:14 pm chen jia wrote:> Hi there, > > I am looking for a function that takes a vector as input and generates > all pair wise combination of the elements of the input vector. > > For example, the input vector is c(1,2,3,4). The output vector is c(1 > and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4). The > representation here is generic. > > I can use a for loop to do it, but it gets slow when the number of > elements of the input vector is large. > > Best, > Jia