Applies a transformation strategy to a numeric vector of edge
weights before passing them to pagerank. Useful for
converting link positions, click counts, or other raw signals into weights
suitable for the PageRank random surfer model.
Usage
transform_weights(
x,
method = c("none", "rank_linear", "zipf", "log", "minmax", "percentile"),
alpha = 1,
offset = 1,
floor_value = 0.01,
descending = TRUE
)Arguments
- x
Numeric vector of raw weights (e.g., link positions on a page, GA4 click counts, or any positive numeric signal).
- method
Character, the transformation strategy. One of:
"none"Return
xunchanged."rank_linear"Convert to rank order (1 = highest value), then assign linearly decreasing weights:
weight = (n - rank + 1) / n. Position 1 gets 1.0, position n gets1/n."zipf"Convert to rank order, then apply Zipf's law:
weight = 1 / rank^alpha. Position 1 gets 1.0, position 2 gets1/2^alpha, etc. Controlled by thealphaparameter (default 1)."log"Apply
log(x + offset)to compress large ranges (e.g., GA4 click counts spanning 1 to 100,000). Theoffsetparameter (default 1) avoidslog(0)."minmax"Scale to the
[0, 1]range using min-max normalisation. A small floor (floor_value, default 0.01) is added so that the lowest-weighted edge still carries some weight rather than zero."percentile"Map values to their empirical percentile (0–1). Robust to extreme outliers.
- alpha
Numeric, exponent for the
"zipf"method. Default1.0. Higher values make the drop-off steeper (position 1 dominates more).- offset
Numeric, added to
xbefore the"log"transform. Default1(so that zero-valued inputs producelog(1) = 0rather than-Inf).- floor_value
Numeric, minimum weight for the
"minmax"method. Default0.01.- descending
Logical. For rank-based methods (
"rank_linear","zipf"), whether higher input values get higher weights. DefaultTRUE(e.g., if the input is click counts, more clicks = higher weight). Set toFALSEwhen the input is link position on a page (position 1 = most valuable, but numerically smallest).
Value
Numeric vector of the same length as x with transformed
weights. NA values in x are preserved as NA in
the output.
Examples
# Link positions on a page (1 = top, most valuable)
positions <- c(1, 2, 3, 4, 5)
transform_weights(positions, "rank_linear", descending = FALSE)
#> [1] 1.0 0.8 0.6 0.4 0.2
transform_weights(positions, "zipf", alpha = 1, descending = FALSE)
#> [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000
transform_weights(positions, "zipf", alpha = 2, descending = FALSE)
#> [1] 1.0000000 0.2500000 0.1111111 0.0625000 0.0400000
# GA4 click counts (wide range)
clicks <- c(50000, 12000, 800, 150, 3)
transform_weights(clicks, "log")
#> [1] 10.819798 9.392745 6.685861 5.017280 1.386294
transform_weights(clicks, "minmax")
#> [1] 1.00000000 0.24755485 0.02578155 0.01291077 0.01000000
transform_weights(clicks, "zipf")
#> [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000
# Use with pagerank()
edges <- data.frame(
from = c("Home", "Home", "Home"),
to = c("About", "Blog", "Contact"),
position = c(1, 2, 5)
)
edges$weight <- transform_weights(edges$position, "zipf",
descending = FALSE
)
# pagerank(edges, weight_col = "weight", clean_edge_urls = FALSE)