Skip to contents

Transforms xmap objects into incidence matrix where the rows are indexed by the from values and the columns are indexed by to values. Drops any additional variables.

Usage

xmap_to_matrix(.xmap, sparse, ...)

# S3 method for xmap_df
xmap_to_matrix(.xmap, sparse = TRUE, ...)

Arguments

.xmap

an xmap object

sparse

logical specifying if the result should be a sparse matrix. Defaults to TRUE.

...

Reversed for passing arguments to stats::xtabs

Value

A matrix or sparse matrix object

Methods (by class)

  • xmap_to_matrix(xmap_df): Coerce a xmap_df to a Matrix

See also

Other xmap coercion: xmap_to_named_vector()

Examples

abc_xmap <- data.frame(
  stringsAsFactors = FALSE,
  origin = c(
    "a", "b", "c", "d", "e",
    "f", "g", "h", "i", "i", "j", "j", "j"
  ),
  dest = c(
    "AA", "AA", "AA", "AA",
    "BB", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II"
  ),
  link = c(1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.3, 0.3, 0.4)
) |>
  as_xmap_df(origin, dest, link)
#> Dropping any additional columns in `data.frame(stringsAsFactors = FALSE, origin
#> = c("a", "b", "c", `, ` "d", "e", "f", "g", "h", "i", "i", "j", "j", "j"), dest
#> = c("AA", `, ` "AA", "AA", "AA", "BB", "BB", "CC", "DD", "EE", "FF", "GG", `, `
#> "HH", "II"), link = c(1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.3, `, and ` 0.3,
#> 0.4))`
#>  To silence set `.drop_extra = TRUE`
xmap_to_matrix(abc_xmap)
#> 10 x 9 sparse Matrix of class "dgCMatrix"
#>       dest
#> origin AA BB CC DD  EE  FF  GG  HH  II
#>      a  1  .  .  . .   .   .   .   .  
#>      b  1  .  .  . .   .   .   .   .  
#>      c  1  .  .  . .   .   .   .   .  
#>      d  1  .  .  . .   .   .   .   .  
#>      e  .  1  .  . .   .   .   .   .  
#>      f  .  1  .  . .   .   .   .   .  
#>      g  .  .  1  . .   .   .   .   .  
#>      h  .  .  .  1 .   .   .   .   .  
#>      i  .  .  .  . 0.5 0.5 .   .   .  
#>      j  .  .  .  . .   .   0.3 0.3 0.4