get_vertex_attr_aggregate: Compute Aggregate of Vertex Attributes

Description Usage Arguments Value Author(s) Examples

View source: R/get_vertex_attr_aggregate.R

Description

Calculate the group by aggregate of a specific vertex attribute

Usage

1
get_vertex_attr_aggregate(g, attr, groups, func, ...)

Arguments

g

An 'igraph' object

attr

Character value, indicating the vertex attribute to be aggregated

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
# 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 aggregate of vertex attribute
# Returns a vector object of length 2
# The names represent postcode area
# The value represent total population of each postcode area
get_vertex_attr_aggregate(g = BristolBathGraph,
                          groups = area,
                          attr = "population", 
                          func = sum)

# Optional arguments can be passed onto `func` using `...`.
# This will calculate the percentiles of the population of each postcode area
# Returns a matrix object of 2 columns * 4 rows.
# The columns represent postcode areas
# The rows are names as '20%', '40%', '60%' and '80%'
# You may use `names=FALSE` to suppress row names (see documentation for `?quantile()`)
get_vertex_attr_aggregate(g = BristolBathGraph,
                          groups = area,
                          attr = "population", 
                          func = quantile,
                          probs = c(0.2, 0.4, 0.6, 0.8))

get_vertex_attr_aggregate(g = BristolBathGraph,
                          groups = area,
                          attr = "population", 
                          func = quantile,
                          probs = c(0.2, 0.4, 0.6, 0.8),
                          names = FALSE)

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