get_matrix_aggregate: Compute Aggregate of Matrix Values

Description Usage Arguments Value Author(s) Examples

View source: R/get_matrix_aggregate.R

Description

Calculate the group by aggregate of a matrix

Usage

1
get_matrix_aggregate(g, m, groups, func, ...)

Arguments

g

An 'igraph' object

m

An adjacency matrix of numeric values. The number of column and rows must be identical. All rows and columns must be named. Ideally this is a 'N*N' shortest path distance matrix.

groups

Vector of character or factor, which indicate the groups to be aggregated

func

Aggregation function

...

Optional arguments to be passed onto the aggregation function

Value

A vector or matrix object

Author(s)

Timothy Wong, timothy.wong@hotmail.co.uk

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Use the postcode sector to derive the area
# Example: BS = Bristol; BA = Bath
library(igraph)
library(stringr)
area <- factor(str_extract(vertex_attr(BristolBathGraph, "name"),"^[A-Z]*"),
               labels = c(BS="Bristol", BA="Bath"))

# Calculate the shortest paths for all vertexes
my_quickest_paths <- distances(graph = BristolBathGraph,
                               weights = edge_attr(BristolBathGraph,"duration"))

# Calculate the maximum travel time between any two vertexes within a postcode area
# Returns a vector object of length 2
# The names represent postcode area
# The value represent the maximum travel duration of each postcode area
get_matrix_aggregate(g = BristolBathGraph, 
                     m = my_quickest_paths,
                     groups = area,
                     func = max)

# Optional arguments can be passed onto `func` using `...`.
# This will calculate the percentiles of the travel duration of each postcode area
# Returns a matrix object of 2 columns * 5 rows.
# The columns represent postcode areas
# The rows are names as '10%', '30%', '50%', '70%' and '90%'
# You may use `names=FALSE` to suppress row names (see documentation for `?quantile()`)
get_matrix_aggregate(g = BristolBathGraph, 
                     m = my_quickest_paths,
                     groups = area,
                     func = quantile,
                     probs = c(0.1, 0.3, 0.5, 0.7, 0.9))

get_matrix_aggregate(g = BristolBathGraph, 
                     m = my_quickest_paths,
                     groups = area,
                     func = quantile,
                     probs = c(0.1, 0.3, 0.5, 0.7, 0.9),
                     names = FALSE)

timothywong731/GeosptNet documentation built on Dec. 23, 2021, 10:57 a.m.