Add weights to unweighted links
add_weights.Rd
Attach column of weights to a table of unweighted source-target links.
If calculating equal fractional weights, uses distinct from
-to
pairs.
The resultant weighted links can be verified or coerced into xmap
.
Usage
add_weights_unit(df, weights_into = "weights")
add_weights_equal(df, from, to, weights_into = "weights")
add_weights_prop(df, from, to, prop, weights_into = "weights")
Arguments
- df
a data frame-like object with at least two columns
- weights_into
character string naming new column to store link weights in
- from, to
Columns in
x
specifying the source and target nodes- prop
numeric column containing reference values to calculate fractional weights from. Weights are calculated as
prop/sum(prop)
where the sum is calculated separately for each set of links coming out of a givenfrom
node.
Functions
add_weights_unit()
: Attach column of unit weights (i.e. ones)add_weights_equal()
: Attach equal fractional weights byfrom
groupadd_weights_prop()
: Attach weights byfrom
group using proportion referenceprop
Examples
# simple unit weights
AUS_pairs <- list(AUS = c("NSW", "QLD", "SA", "TAS", "VIC", "WA", "ACT", "NT")) |>
as_pairs_from_named(names_to = "ctr", values_to = "state")
AUS_pairs |>
add_weights_unit(weights_into = "weights")
#> # A tibble: 8 × 3
#> ctr state weights
#> <chr> <chr> <dbl>
#> 1 AUS NSW 1
#> 2 AUS QLD 1
#> 3 AUS SA 1
#> 4 AUS TAS 1
#> 5 AUS VIC 1
#> 6 AUS WA 1
#> 7 AUS ACT 1
#> 8 AUS NT 1
# fractional weights
animal_pairs <- list(
MAMM = c("elephant", "whale", "monkey"),
REPT = c("lizard", "turtle"),
CRUS = c("crab")
) |>
as_pairs_from_named("class", "animal")
animal_pairs |>
add_weights_equal(from = class, to = animal)
#> # A tibble: 6 × 3
#> class animal weights
#> <chr> <chr> <dbl>
#> 1 MAMM elephant 0.333
#> 2 MAMM whale 0.333
#> 3 MAMM monkey 0.333
#> 4 REPT lizard 0.5
#> 5 REPT turtle 0.5
#> 6 CRUS crab 1
# proportional weights
data.frame(
recipe = c(rep("cake", 4), rep("pasta", 2)),
ingredients = c(c("flour", "sugar", "eggs", "milk"), c("flour", "water")),
grams = c(c(500, 250, 200, 250), c(250, 150))
) |>
add_weights_prop(recipe, ingredients, grams)
#> # A tibble: 6 × 4
#> recipe ingredients grams weights
#> <chr> <chr> <dbl> <dbl>
#> 1 cake flour 500 0.417
#> 2 cake sugar 250 0.208
#> 3 cake eggs 200 0.167
#> 4 cake milk 250 0.208
#> 5 pasta flour 250 0.625
#> 6 pasta water 150 0.375