Pairs, given as rows of a matrix, data.frame, or list, are processed to return a list of paths, each identifying the connected pairs in the rows of x.

connect_pairs(x, duplicate.rm = FALSE)

Arguments

x

two-column matrix, data.frame, or a list containing vectors of length two representing the pairs to be connected.

duplicate.rm

logical indicating whether equal pairs (up to permutation) are to be omitted.

Value

A list each of whose elements give a path of connected pairs. Each list element is a vector of length at least 2 (longer vectors > 2 in length identify the pairs connected in a path).

See also

zenplot() which provides the zenplot.

Other tools related to constructing zenpaths: extract_pairs(), graph_pairs(), groupData(), indexData(), zenpath()

Author

Marius Hofert and Wayne Oldford

Examples

## First something simple.
(pairs <- matrix(c(1,2,2,3,3,5,5,7,8,9), ncol = 2, byrow = TRUE))
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    2    3
#> [3,]    3    5
#> [4,]    5    7
#> [5,]    8    9
## Connect pairs into separate paths defined by the row order.
connect_pairs(pairs)
#> [[1]]
#> [1] 1 2 3 5 7
#> 
#> [[2]]
#> [1] 8 9
#> 

## Now something different
nVars <- 5
pairs <- expand.grid(1:nVars, 1:nVars)
## and take those where
(pairs <- pairs[pairs[,1] < pairs[,2],])
#>    Var1 Var2
#> 6     1    2
#> 11    1    3
#> 12    2    3
#> 16    1    4
#> 17    2    4
#> 18    3    4
#> 21    1    5
#> 22    2    5
#> 23    3    5
#> 24    4    5
connect_pairs(pairs)
#> [[1]]
#> [1] 2 1 3 2
#> 
#> [[2]]
#> [1] 1 4 2
#> 
#> [[3]]
#> [1] 3 4
#> 
#> [[4]]
#> [1] 1 5 2
#> 
#> [[5]]
#> [1] 3 5 4
#> 

## Something more complicated.
## Get weights
set.seed(27135)
x <- runif(choose(nVars,2)) # weights

## We imagine pairs identify edges of a graph with these weights
## Get a zenpath ordering the edges based on weights
(zp <- zenpath(x, pairs = pairs, method = "strictly.weighted"))
#> [[1]]
#> [1] 2 3
#> 
#> [[2]]
#> [1] 3 5
#> 
#> [[3]]
#> [1] 2 5
#> 
#> [[4]]
#> [1] 3 4
#> 
#> [[5]]
#> [1] 4 5
#> 
#> [[6]]
#> [1] 1 5
#> 
#> [[7]]
#> [1] 2 4
#> 
#> [[8]]
#> [1] 1 2
#> 
#> [[9]]
#> [1] 1 3
#> 
#> [[10]]
#> [1] 1 4
#> 

## And connect these giving the list of paths
connect_pairs(zp)
#> [[1]]
#> [1] 2 3 5 2
#> 
#> [[2]]
#> [1] 3 4 5 1
#> 
#> [[3]]
#> [1] 4 2 1 3
#> 
#> [[4]]
#> [1] 1 4
#>