File: combn2.R

package info (click to toggle)
r-cran-combinat 0.0-7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 124 kB
  • sloc: makefile: 1
file content (38 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"combn2"<-
function(x, n)
{
#   DATE WRITTEN:  14 April 1994           LAST REVISED:  14 April 1994
#   AUTHOR:  Scott D. Chasalow
#
#   DESCRIPTION:
#         Generate all combinations of the elements of x taken two at a time. 
#         If x is missing,  generate all combinations of 1:n taken two
#         at a time (that is,  the indices of x that would give all 
#         combinations of the elements of x if x with length n had been given).
#         Exactly one of arguments "x" and "n" should be given.
#
	if(!missing(x)) {
		if(!missing(n))
			warning(paste("Only one of arguments x and n allowed;", 
				"argument n was ignored"))
		n <- length(x)
	}
	else if(missing(n))
		stop("Arguments \"x\" and \"n\" both missing")
	if(length(n) > 1) {
		warning(paste("Argument n has", length(n), 
			"elements: only the first used"))
		n <- n[1]
	}
	if(n == 0)
		return(NULL)
	rmat <- array(seq(length = n), c(n, n))	# row(matrix(0,n,n))
	cmat <- t(rmat)	# col(matrix(0,n,n))
	lower.t <- rmat > cmat	# lower.tri(matrix(0,n,n))
	i1 <- cmat[lower.t]
	i2 <- rmat[lower.t]
	if(missing(x))
		cbind(i1, i2)
	else cbind(x[i1], x[i2])
}