makes index ids for the provided hy object. These can be used for graph traversal algorithms such that the row number and id are equal.
Usage
make_index_ids(x, long_form = FALSE)
# S3 method for class 'data.frame'
make_index_ids(x, long_form = FALSE)
# S3 method for class 'hy'
make_index_ids(x, long_form = FALSE)
Arguments
- x
data.frame network compatible with hydroloom_names.
- long_form
logical if TRUE, return will be a long-form version of the
to_list
. This form can be converted to the default list format with format_index_ids.
Value
list containing named elements: to
: adjacency matrix lengths
:
vector indicating the number of connections from each node, and: to_list
:
a data.frame with an id
, indid
and a toindid
list column. If long_form
= TRUE, return will be a long form data.frame with no list column as in to_list
.
NOTE: the long_form output should be used with caution as indid may not
correspond to row number.
Examples
x <- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
toid = c(2, 3, 4, 5, 0, 7, 8, 9, 4))
make_index_ids(x)
#> $to
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> [1,] 2 3 4 5 0 7 8 9 4
#>
#> $lengths
#> 1 2 3 4 5 6 7 8 9
#> 1 1 1 1 1 1 1 1 1
#>
#> $to_list
#> id indid toindid
#> 1 1 1 2
#> 2 2 2 3
#> 3 3 3 4
#> 4 4 4 5
#> 5 5 5 0
#> 6 6 6 7
#> 7 7 7 8
#> 8 8 8 9
#> 9 9 9 4
#>
x <- hy(sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom")))
x <- add_toids(x, return_dendritic = FALSE)
x <- make_index_ids(x)
names(x)
#> [1] "to" "lengths" "to_list"
class(x$to)
#> [1] "matrix" "array"
class(x$lengths)
#> [1] "numeric"
class(x$to_list)
#> [1] "data.frame"
is.list(x$to_list$toindid)
#> [1] TRUE