Get the names or ID's of all vertices with whom the focal vertex is connected

extract_neighbors(x, vertex, type = c("out", "in", "all"))

Arguments

x

graph object if class igraph or network

vertex

number or name (if the graph has vertex names) of a single vertex

type

the type of neighborhood to be returned

Value

vector with vertex ID's or names of the neighbors

Details

When mode == "out" the function returns all vertices that receive an edge from vertex.

When mode == "in" the function returns all vertices that send an edge to vertex.

When mode == "all" all vertices with an edge to or from vertex are returned.

In an undirected graph, mode will always be taken to be "all".

Note that vertex needs to be a single vertex, not a vector.

Examples

m <- matrix(0, 3, 3)
m[1, 2] <- 1; m[2, 3] <- 1; m[3, 1] <- 1
g_i <- snafun::to_igraph(m)
extract_neighbors(g_i, 1, "out")
#> [1] 2
extract_neighbors(g_i, 1, "in")
#> [1] 3
extract_neighbors(g_i, 1, "all")
#> [1] 2 3

g_n <- snafun::to_network(m)
extract_neighbors(g_n, 1, "out")
#> [1] 2
extract_neighbors(g_n, 1, "in")
#> [1] 3
extract_neighbors(g_n, 1, "all")
#> [1] 2 3